I am currently making a program to read each char in a text file and adding it into an array.
The problem is that it can't find the file. I have tried to print out the absolute path and the path is correct.
I can't see what I'm going to change.
Here's my code:
package trust.src; import java.awt.List; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public class Level { private int level; private char[][] tiles; private String[] lines; public Level() { tiles = new char[20][30]; level = 1; getLevel(); } public void getLevel() { String fileName = "level" + Integer.toString(level) + ".txt"; try { //creates the file. File file = new File("res/levels/" + fileName); String line; int index = 0; BufferedReader reader = new BufferedReader(new FileReader(file)); //reads the lines and add them into a String array. while ((line = reader.readLine()) != null) { lines[index] = line; index++; } //reads each char in each line. for (int i = 0; i < lines.length; i++) { for (int j = 0; j < lines[i].length(); j++) { tiles[i][j] = lines[i].charAt(j); System.out.println(lines[i]); } } }catch(Exception ex) {System.out.println("Error while loading level"); ex.printStackTrace();} } }
and this is the output i get:
Error while loading level java.lang.NullPointerException at trust.src.Level.getLevel(Level.java:30) at trust.src.Level.<init>(Level.java:17) at trust.src.Board.<init>(Board.java:8) at trust.src.Trust.<init>(Trust.java:13) at trust.src.Trust.main(Trust.java:18)
I dont know what i should do.
Please help me!