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

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

问题描述

我试图做一个2D游戏的瓷砖,使阵列抱着我得到一个NullPointerException异常瓷砖的时候,这里是一些code的。
很抱歉,如果这个格式不,第一个定时器

公共类世界{

  //适用于在何处放置图像和数据的矩形
INT [] [] worldDat =新INT [25] [25];
//保持矩形的检查与玩家互动
矩形[] []块=新矩形[25] [25];
//保存要涂块的图片
图片[] [] = blockImage新的图像[25] [25];
//保存位置在屏幕上进行心疼
INT [] [] =位置INT新[25] [25];
//枚举持有的那种块的图像和其他的东西
EWorldBlocks eBlocks;//设置上面列出的所有阵列
公共无效setupAll(){    的for(int i = 0; I< 24;我++){        对于(INT E = 0; E< 24,E ++){
            块[I] [E] .setBounds(E * 20,我* 20,20,20);
            块[I] [E] .setLocation(E * 20,我* 20);            如果(worldDat [I] [E] == 6){
                blockImage [I] [E] = getRandomGrass();
            }否则如果(worldDat [I] [E] == 0){
                blockImage [I] [E] = NULL;
            }其他{
            blockImage [I] [E] = eBlocks.intToImage(worldDat [I] [E]);
            }
        }
    }
}//用来得到一个随机块
私人形象getRandomGrass()
{
    随机兰特=新的随机();    INT R = rand.nextInt(2);
     - [R ++;    返回eBlocks.intToImage(R);
}
公共世界(INT [] [] worldDat){
    this.worldDat = worldDat;
}}

那么就是所谓的在这个类(我相信它的问题的一部分)

公共类worldDraw {

  //另外,如果有更好的方式来做到这一点,不要告诉
各级水平=新的水平();
静态水平sLevels =新的水平();
世界1级;
静态世界sLevel1 =新的世界(sLevels.getLevel1());//呼吁在面板paint方法
公共无效画(Graphics2D的G2){
    sLevel1.setupAll();
    的for(int i = 0; I< 24;我++){
        对于(INT E = 0; I< 24;我++){
            g2.drawImage(level1.blockImage [I] [E],E * 25,我* 25,NULL);
        }
    }}//持有水平
公共worldDraw(){
    1级=新的世界(levels.getLevel1());
}
}


解决方案

当你创建一个对象数组,你正在创建引用数组,但你不分配引用。你必须设法使用它们之前,首先做到这一点。可以把它当作是类似于创建一个鸡蛋纸箱。你不能使用任何鸡蛋,直到你先填写纸箱蛋。因此,例如您的块阵列,您首先需要分配对象矩形每个项目在数组中后,才能调用它们的方法。这通常是在完成了循环。例如,

 的for(int i = 0; I< 24;我++){
    对于(INT E = 0; E< 24,E ++){
        块[I] [E] =新的Rectangle(....); // ...
        块[I] [E] .setBounds(E * 20,我* 20,20,20);
        块[I] [E] .setLocation(E * 20,我* 20);

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

public class World {

//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)

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());
}
}

解决方案

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);

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

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