Creating a custom 2D array Tile Map with text file using line.Split gives
null exception error
I have written the following code to read a tile map from a text file,
however I am getting a null point exception error for a reason I can't
figure out.
Code of the reader:
BufferedReader br = new BufferedReader(new FileReader(s));
readMapWidth = Integer.parseInt(br.readLine());
readMapHeight = Integer.parseInt(br.readLine());
map = new int[readMapHeight][readMapWidth];
for(int row = 0; row < readMapHeight; row++) {
String line = br.readLine();
System.out.println(line);
String[] tileValues = line.split(",");
for(int col = 0; col < readMapWidth; col++){
map[row][col] = Integer.parseInt(tileValues[col]);
}
}
}
The text file content:
What the command console returns as an error:
java.lang.NullPointerException
at TileMap.<init>(TileMap.java:58)
which is line:
String[] tileValues = line.split(",");
The lines read perfectly and I can draw the map to the screen. however I
need an actually stored 2d array to use for pathfinding later, but this
line is returning a null value and I can't understand why.
Here is what the "System.out.println(line)" returns that confuses me:
[final line of the map here, map lines print as normal]
null
The null? I don't understand, my text file only has 27 lines where is the
null exception regarding split coming from?
No comments:
Post a Comment