初始化二维对象数组时出现空指针异常 [Java] [英] null pointer exception when initializing a 2D object array [Java]

查看:24
本文介绍了初始化二维对象数组时出现空指针异常 [Java]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 2D 瓷砖游戏,当制作包含瓷砖的数组时,我得到一个 NullPointerException,这是一些代码.对不起,如果这是格式错误,第一次

I'm trying to make a 2D tile game and when making the arrays holding the tiles I get a NullPointerException, here is some of the code. Sorry if this is poorly formatted, first timer

公开课世界{

//holds data for where to place images and rectangles
int[][] worldDat = new int[25][25]; 
//hold rectangles for checking interaction with player
Rectangle[][] blocks = new Rectangle[25][25];
//holds block's images to be painted
Image[][] blockImage = new Image[25][25];
//holds position to be pained on screen
int[][] location = new int[25][25];
//enumeration holding block's images and other things of the sort
EWorldBlocks eBlocks;

//sets all of the arrays listed above
public void setupAll(){

    for(int i = 0; i < 24; i++){

        for(int e = 0; e < 24; e++){                    
            blocks[i][e].setBounds(e * 20, i * 20, 20, 20);
            blocks[i][e].setLocation(e*20, i*20);

            if(worldDat[i][e] == 6){
                blockImage[i][e] = getRandomGrass();
            }else if(worldDat[i][e] == 0){
                blockImage[i][e] = null;
            }else{
            blockImage[i][e] = eBlocks.intToImage(worldDat[i][e]);
            }
        }
    }
}   

//used to get a random block
private Image getRandomGrass()
{
    Random rand = new Random();

    int r = rand.nextInt(2);
    r++;

    return eBlocks.intToImage(r);
}


public World(int[][] worldDat) {
    this.worldDat = worldDat;
}

}

然后在这个类中调用(我相信这是问题的一部分)

Then that is called in this class (i believe its part of the problem)

公开课 worldDraw{

public class worldDraw{

//ALSO if there is a better way to do this, do tell
levels levels = new levels();
static levels sLevels = new levels();
World level1;
static World sLevel1 = new World(sLevels.getLevel1());

//called in paint method for panel
public void draw(Graphics2D g2){
    sLevel1.setupAll();
    for(int i = 0; i < 24; i++){
        for(int e = 0; i < 24; i++){
            g2.drawImage(level1.blockImage[i][e], e*25, i*25, null);
        }
    }

}

//holds levels
public worldDraw() {        
    level1 = new World(levels.getLevel1());
}
}

推荐答案

当您创建对象数组时,您正在创建一个引用数组,但您并未分配引用.在尝试使用它们之前,您必须先执行此操作.把它想象成类似于创建一个鸡蛋盒.在您先将鸡蛋装满纸箱之前,您不能使用任何鸡蛋.因此,例如您的 blocks 数组,您首先需要将 Rectangle 对象分配给数组中的每个项目,然后才能对它们调用方法.这通常在 for 循环中完成.例如,

When you create an object array, you are creating an array of references, but you are not assigning the references. You must do this first before trying to use them. Think of it as being similar to creating an egg carton. You can't use any eggs until you first fill the carton with eggs. So for instance your blocks array, you first need to assign Rectangle objects to each item in the array before you can call methods on them. This is usually done in a for loop. e.g.,

for(int i = 0; i < 24; i++){
    for(int e = 0; e < 24; e++){       
        blocks[i][e] = new Rectangle(....); //...             
        blocks[i][e].setBounds(e * 20, i * 20, 20, 20);
        blocks[i][e].setLocation(e*20, i*20);

这篇关于初始化二维对象数组时出现空指针异常 [Java]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆