初始化2D对象数组[Java]时出现空指针异常

初始化2D对象数组[Java]时出现空指针异常,java,arrays,object,nullpointerexception,rectangles,Java,Arrays,Object,Nullpointerexception,Rectangles,我正在尝试制作一个2D平铺游戏,当制作包含平铺的数组时,我得到一个NullPointerException,下面是一些代码。 抱歉,如果这是格式不正确,第一个计时器 公共阶级世界{ //holds data for where to place images and rectangles int[][] worldDat = new int[25][25]; //hold rectangles for checking interaction with player Rectangle[][]

我正在尝试制作一个2D平铺游戏,当制作包含平铺的数组时,我得到一个NullPointerException,下面是一些代码。 抱歉,如果这是格式不正确,第一个计时器

公共阶级世界{

//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;
}

}
//保存用于放置图像和矩形的位置的数据
int[]worldDat=新int[25][25];
//按住矩形以检查与玩家的交互
矩形[][]块=新矩形[25][25];
//保存要绘制的块的图像
图像[][]块图像=新图像[25][25];
//在屏幕上保持要绘制的位置
int[][]位置=新int[25][25];
//包含块的图像和其他类似内容的枚举
伊沃尔德布洛克;
//设置上面列出的所有数组
公共无效设置全部(){
对于(int i=0;i<24;i++){
对于(int e=0;e<24;e++){
块[i][e]。立根(e*20,i*20,20,20);
块[i][e]。设置位置(e*20,i*20);
if(worldDat[i][e]==6){
blockImage[i][e]=getRandomGrass();
}else if(worldDat[i][e]==0){
块映像[i][e]=空;
}否则{
blockImage[i][e]=eBlocks.intToImage(worldDat[i][e]);
}
}
}
}   
//用于获取随机块
私有映像getRandomGrass()
{
Random rand=新的Random();
int r=兰特·奈克斯汀(2);
r++;
返回eBlocks.intToImage(r);
}
公共世界(int[][]世界数据){
this.worldDat=worldDat;
}
}
然后在这个类中调用(我相信这是问题的一部分)

公共级世界抽奖{

//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());
}
}
//如果有更好的方法,请告诉我
级别=新级别();
静态级别sLevels=新级别();
世界水平1;
静态世界sLevel1=新世界(sLevels.getLevel1());
//在面板的绘制方法中调用
公共空白绘图(图形2D g2){
sLevel1.setupAll();
对于(int i=0;i<24;i++){
对于(int e=0;i<24;i++){
g2.drawImage(级别1.blockImage[i][e],e*25,i*25,空);
}
}
}
//保持水平
公共worldDraw(){
level1=新世界(levels.getLevel1());
}
}

创建对象数组时,您正在创建引用数组,但未指定引用。在尝试使用它们之前,必须先执行此操作。可以将其视为类似于创建一个鸡蛋盒。你不能用鸡蛋,除非你先把纸箱装满鸡蛋。例如,在块数组中,首先需要将矩形对象分配给数组中的每个项,然后才能对它们调用方法。这通常是在for循环中完成的。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);
for(int i=0;i<24;i++){
对于(int e=0;e<24;e++){
块[i][e]=新矩形(..);/。。。
块[i][e]。立根(e*20,i*20,20,20);
块[i][e]。设置位置(e*20,i*20);

当你创建一个对象数组时,你正在创建一个引用数组,但是你没有分配引用。在尝试使用它们之前,你必须先这样做。把它想象成创建一个鸡蛋盒。在你第一次用鸡蛋填满盒子之前,你不能使用任何鸡蛋。因此,例如,你的块数组,你首先不需要o将矩形对象分配给数组中的每个项,然后才能对其调用方法。这通常在for循环中完成。例如

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);
for(int i=0;i<24;i++){
对于(int e=0;e<24;e++){
块[i][e]=新矩形(..);/。。。
块[i][e]。立根(e*20,i*20,20,20);
块[i][e]。设置位置(e*20,i*20);

您需要知道Java不像C

执行此操作时:

Rectangle[][] blocks = new Rectangle[25][25];
块2D数组中的所有引用都为空,直到调用new并为其提供引用为止

因此,您必须这样做:

for(int i = 0; i < 24; i++){
    for(int e = 0; e < 24; e++){             
        blocks[i][e] = new Rectangle(); // I don't know what arguments it takes.       
        blocks[i][e].setBounds(e * 20, i * 20, 20, 20);
        blocks[i][e].setLocation(e*20, i*20);
for(int i=0;i<24;i++){
对于(int e=0;e<24;e++){
blocks[i][e]=new Rectangle();//我不知道它需要什么参数。
块[i][e]。立根(e*20,i*20,20,20);
块[i][e]。设置位置(e*20,i*20);

您需要知道Java不像C

执行此操作时:

Rectangle[][] blocks = new Rectangle[25][25];
块2D数组中的所有引用都为空,直到调用new并为其提供引用为止

因此,您必须这样做:

for(int i = 0; i < 24; i++){
    for(int e = 0; e < 24; e++){             
        blocks[i][e] = new Rectangle(); // I don't know what arguments it takes.       
        blocks[i][e].setBounds(e * 20, i * 20, 20, 20);
        blocks[i][e].setLocation(e*20, i*20);
for(int i=0;i<24;i++){
对于(int e=0;e<24;e++){
blocks[i][e]=new Rectangle();//我不知道它需要什么参数。
块[i][e]。立根(e*20,i*20,20,20);
块[i][e]。设置位置(e*20,i*20);

我该怎么做?我该怎么做?