Java 解析文件以制作游戏地图时出现NullPointerException
我正在做一个2D的小游戏,从上面看。我正在尝试这样做: 1我有一个本地文件,地图布局为.txt 2解析此文件并在游戏中制作地图布局 在屏幕上画所有的东西 我认为这样成功地解析了文件并将其放入应用程序,但它无法绘制,引发了NullPointerException。我知道这里有些东西是空的,但我看不出是什么和在哪里——从我的观点来看,一切都是它应该在的地方 一些代码:Java 解析文件以制作游戏地图时出现NullPointerException,java,parsing,dictionary,game-engine,Java,Parsing,Dictionary,Game Engine,我正在做一个2D的小游戏,从上面看。我正在尝试这样做: 1我有一个本地文件,地图布局为.txt 2解析此文件并在游戏中制作地图布局 在屏幕上画所有的东西 我认为这样成功地解析了文件并将其放入应用程序,但它无法绘制,引发了NullPointerException。我知道这里有些东西是空的,但我看不出是什么和在哪里——从我的观点来看,一切都是它应该在的地方 一些代码: BufferedReader br = new BufferedReader(new FileReader(s) );
BufferedReader br = new BufferedReader(new FileReader(s) );
mapwidth = Integer.parseInt(br.readLine());
mapheight = Integer.parseInt(br.readLine());
String delimiters = " ";
for (row = 0; row < mapheight; row++){
String line = br.readLine();
String[] tokens = line.split(delimiters);
for (col = 0; col < mapwidth ; col++){
map[row][col] = Integer.parseInt(tokens[col]);
}
}
最后一行抛出一个NullPointerException。
平铺是我游戏中的一个单场。它有四个值-BuffereImage、blocked、walkable和Destructurable最后三个是布尔值。
另外,我是否将用于生成对象的代码与解析代码放在一起也没关系。但我还是有例外。
我知道这里少了些什么,但我不知道是什么
稍后我要绘制所有瓷砖:
public void paint(Graphics g){
for (row=0; row < mapheight; row++)
for(col = 0; col < mapwidth; col++){
g.drawImage(
tile[row][col].getImage(),
row*tileSize,
col*tileSize,
null
);
我想它可能有一些错误,但我不确定。我想做的是,获取每个瓷砖的图像,并将其绘制在适当的行和列中。每个字段都是一个平方长度=tileSize
编辑
谢谢大家
@Thierry-调试后,我发现tile仍然为null,并且没有填充。因此,我创建了一个tile的空实例,并将这两个函数放在一起。现在它看起来像这样,并且工作正常:
public GameTileMap(String s, int tileSize){
this.tileSize = tileSize;
try{
BufferedReader br = new BufferedReader(new FileReader(s) );
mapwidth = Integer.parseInt(br.readLine());
mapheight = Integer.parseInt(br.readLine());
map = new int[mapheight][mapwidth];
tile = new Tile[mapheight][mapwidth];
int mh = mapheight;
int mw = mapwidth ;
String delimiters = " ";
for (row = 0; row < mh; row++){
String line = br.readLine();
String[] tokens = line.split(delimiters);
for (col = 0; col < mw ; col++){
map[row][col] = Integer.parseInt(tokens[col]);
}
}
String wall ="Images/wall.jpg";
String brick = "Images/brick.jpg";
String grass ="Images/grass.jpg";
String water = "Images/water.jpg";
BufferedImage WALL = ImageIO.read(new File(wall));
BufferedImage BRICK = ImageIO.read(new File(brick));
BufferedImage GRASS = ImageIO.read(new File(grass));
BufferedImage WATER = ImageIO.read(new File(water));
for (row = 0; row < mh; row++){
for (col = 0; col < mw ; col++){
if (map[row][col] == 0) { tile[row][col] = new Tile(GRASS, false, true, false); }
else if (map[row][col] == 1) {tile[row][col] = new Tile(BRICK, true, false, true);}
else if (map[row][col] == 2) {tile[row][col] = new Tile(WALL, true, false, false);}
else if (map[row][col] == 3) {tile[row][col] = new Tile(WATER, false, false, false);}
else {tile[row][col] = new Tile(GRASS, false, true, false);};
}
}
}
catch(Exception e){
e.printStackTrace();}
}
执行此命令后:
for (row = 0; row < mapheight; row++){
String line = br.readLine();
String[] tokens = line.split(delimiters);
for (col = 0; col < mapwidth ; col++){
map[row][col] = Integer.parseInt(tokens[col]);
}
}
由于最后一行++和列++,行和列都将越界。这就是为什么在尝试访问map[row][col]==0时出现越界错误的原因
数组中的NullPointerException表示您以某种方式引用了小于0或大于length-1的索引。如果您在以下位置有NPE:
if (map[row][col] == 0) { tile[row][col] = new Tile(WALL, false, true, false);
那么只有4种解决方案:
映射为空
或映射[行]为空
或者,tile为空
或平铺[行]为空
这不会导致ArrayIndexOutOfBounds异常,而不是NullPointerException吗?事实上,这应该会导致两者@这是导致异常的代码:if map[row][col]==0{tile[row][col]=new TileWALL,false,true,false;}?只是确保我理解正确。异常stacktrace包含答案。至少把范围缩小到例外的那一行thrown@Ata-我已经指出了,我已经指出了哪一行引起了麻烦。@dudeprgm-是的,那一行代码
if (map[row][col] == 0) { tile[row][col] = new Tile(WALL, false, true, false);