我正在制作一个2D小游戏,从上面看。我正在尝试这样做:
1)我有一个带有地图布局的本地文件.txt
2)解析此文件并在游戏中进行地图布局
3)在屏幕上绘制所有内容
我(这么认为)成功解析了文件并将其放入应用程序中,但它无法绘制,抛出了 NullPointerException。我知道这里的东西是空的,但我看不到什么和在哪里 - 从我的角度来看,一切都是它应该在的地方。
一些代码:
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]);
}
}
这是将文件解析为程序的功能。前两行读取地图宽度和高度(来自所述文件),然后对于文件中的每个值,我在 map[row][col] 中创建一个点
这是另一个负责为地图的每个点制作对象的函数
String wall ="Images/wall.jpg";
BufferedImage WALL = ImageIO.read(new File(wall));
if (map[row][col] == 0) { tile[row][col] = new Tile(WALL, false, true, false); }
最后一行抛出 NullPointerException。图块是我游戏中的单个字段。它有四个值 - 缓冲图像,块,可行走和可破坏(最后三个是布尔值)。此外,我是否将用于创建对象的代码与解析代码放在一起并不重要。我还是有例外。我知道这里缺少一些东西,但我无法弄清楚是什么。
稍后我想绘制所有瓷砖:
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
);
我认为它可能有一些错误,但我不确定。我想做的是获取每个磁贴的图像并将其绘制在适当的行和列中。每个字段都是一个正方形(长度 = 平铺大小)。
编辑
谢谢大家!
@Thierry - 调试后,我发现磁贴仍然为空并且没有填满。因此,我创建了一个空的磁贴实例,并将这两个函数放在一起。现在它看起来像这样并且工作正常:
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();}
}
如果你在 中有 NPE:
if (map[row][col] == 0) { tile[row][col] = new Tile(WALL, false, true, false);
那么只有4个解决方案:
- 地图为空
- 或地图[行] 为空
- 或磁贴为空
- 或磁贴[行]为空
执行此操作后:
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]);
}
}
row
和 col
都将超出界限,因为最后的 row++ 和 col++。这就是为什么您在尝试访问if (map[row][col] == 0)
时遇到越界错误的原因
数组中的NullPointerException
意味着您以某种方式引用了小于 0 或大于 length()-1 的索引。