java初始化一个空指针_初始化2D对象数组时,空指针异常[Java]

我正在尝试制作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;

}

}

然后在本课程中被称为(我相信这是问题的一部分)

公共类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());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值