简体   繁体   English

为什么我在这里得到“ ArrayIndexOutOfBoundsException”?

[英]Why am I getting “ArrayIndexOutOfBoundsException” here?

Basically title. 基本上是标题。 I create a new fragment of a larger array and I get this error. 我创建了更大数组的新片段,但出现此错误。 I have to create a new fragemnt of a larger array to display it. 我必须创建一个更大的数组的新fragemnt来显示它。 (Think in Pokemon, where the map is the big array but the screen that is shown is the fragment of that map.) Here is the code. (想想口袋妖怪,地图是大数组,但显示的屏幕是该地图的片段。)这是代码。

Where the method is called: 调用方法的位置:

    public void showWorldFragment(){
******ERROR WITH THIS LINE*********
        int[][] map = getWorldFragment(mapPack.map);
        if(map[0][0] == 1){
            c.drawBitmap(balloon, 0, 0, null);
        }
        if(map[0][0] == 2){
            c.drawBitmap(archer, 0, 0, null);
        }
        if(map[1][0] == 1){
            c.drawBitmap(balloon, canvasWidth/3, 0, null);
        }
        if(map[1][0] == 2){
            c.drawBitmap(archer, canvasWidth/3, 0, null);
        }
        if(map[2][0] == 1){
            c.drawBitmap(balloon, 2*(canvasWidth/3), 0, null);
        }
        if(map[2][0] == 2){
            c.drawBitmap(archer, 2*(canvasWidth/3), 0, null);
        }
        if(map[0][1] == 1){
            c.drawBitmap(balloon, 0, canvasHeight/3, null);
        }
        if(map[0][1] == 2){
            c.drawBitmap(archer, 0, canvasHeight/3, null);
        }
        if(map[1][1] == 1){
            c.drawBitmap(balloon, canvasWidth/3, canvasHeight/3, null);
        }
        if(map[1][1] == 2){
            c.drawBitmap(archer, canvasWidth/3, canvasHeight/3, null);
        }
        if(map[2][1] == 1){
            c.drawBitmap(balloon, 2*(canvasWidth/3), canvasHeight/3, null);
        }
        if(map[2][1] == 2){
            c.drawBitmap(archer, 2*(canvasWidth/3), canvasHeight/3, null);
        }
        if(map[0][2] == 1){
            c.drawBitmap(balloon, 0, 2*(canvasHeight/3), null);
        }
        if(map[0][2] == 2){
            c.drawBitmap(archer, 0, 2*(canvasHeight/3), null);
        }
        if(map[1][2] == 1){
            c.drawBitmap(balloon, (canvasWidth/3), 2*(canvasHeight/3), null);
        }
        if(map[1][2] == 2){
            c.drawBitmap(archer, (canvasWidth/3),  2*(canvasHeight/3), null);
        }
        if(map[2][2] == 1){
            c.drawBitmap(balloon, 2*(canvasWidth/3), 2*(canvasHeight/3), null);
        }
        if(map[2][2] == 2){
            c.drawBitmap(archer, 2*(canvasWidth/3), 2*(canvasHeight/3), null);
        }

    }

Where the method is called: 调用方法的位置:

    public int[][] getWorldFragment(int[][] map) {
        int centerX = x;
        int centerY = y;
        if(x == 0){
            centerX = 1;
        }
        if(x == map.length){
            centerX = map.length-1;
        }
        if(y == 0){
            centerY = 1;
        }
        if(y == map.length){
            centerY = map.length-1;
        }
        int[][] tempFragmentArray = new int[][]{
                {map[centerX-1][centerY-1], map[centerX][centerY-1], map[centerX + 1][centerY - 1]},
                {map[centerX-1][centerY], map[centerX][centerY], map[centerX + 1][centerY]},
                {map[centerX-1][centerY+1], map[centerX][centerY+1], map[centerX + 1][centerY + 1]},

        };
        return tempFragmentArray;

    }

The mapPack.map is a larger 5x5 array that just holds basic integer values. mapPack.map是一个更大的5x5数组,仅包含基本整数值。 Does anyone have any idea as to why this happens? 有谁知道为什么会这样吗?

Let me know if any more details are needed. 让我知道是否需要更多详细信息。

Thanks :) 谢谢 :)

I think the inner exception is coming from this line: 我认为内部异常来自此行:

 int[][] tempFragmentArray = new int[][]{
                 {map[centerX-1][centerY-1], map[centerX][centerY-1], map[centerX +      1][centerY - 1]},
                 {map[centerX-1][centerY], map[centerX][centerY], map[centerX + 1][centerY]},
                 {map[centerX-1][centerY+1], map[centerX][centerY+1], map[centerX +      1][centerY + 1]},

    };

The variable x and y look to be inner class members and probably they go to wrong index of your 2D map array. 变量x和y看起来是内部类的成员,并且可能它们进入2D地图数组的错误索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM