0

I am reading a text file representing a maze pattern. each line is read into a 1d char array and then the one 1d array is inserted into the 2d char array.

In the following method get a nullpointerexception at

 line = (input.readLine()).toCharArray(); 

private void fillArrayFromFile() throws IOException { BufferedReader input = new BufferedReader(new FileReader("maze.txt")); //Read the first two lines of the text file and determine the x and y length of the 2d array mazeArray = new char[Integer.parseInt(input.readLine())][Integer.parseInt(input.readLine())]; char [] line = new char[10]; //Add each line of input to mazeArray for (int x = 0; x < mazeArray[0].length; x++) { line = (input.readLine()).toCharArray(); System.out.print(x + " : "); System.out.println(line); mazeArray[x] = line; } } 
1
  • 7
    Because you're at the end of the file? Commented Apr 30, 2012 at 10:34

2 Answers 2

5

BufferedReader.readLine() returns null when there is no more input to be read. See the Java documentation for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

So basically you should fetch the next line, and find out if its null before you transform it to a char array: String lineStr = (input.readLine()); if(lineStr == null) break; line = lineStr.toCharArray();
1

The obvious answer is that input.readLine() is returning null, and since you are calling a method of an object that is pointing to nothing, you are getting a NullPointerException.

The root of this problem though, is the discrepancy between your perception of rows and columns and that of the text file. Also, you are looping through the incorrect index, if I am reading your code correctly.

Here is an example text file:

4 6 a b c d b c d a a d c a b d b a c d a a b a b a 

A good way of thinking about this is to think about the number of rows and columns you have.

For example, the text file above says "I have 4 columns and 6 rows".

That also means x ranges from 0 to 3 and y ranges from 0 to 5.

In your terms, the x-length is the number of columns, and the y-length is the number of rows.

Of course, remember that rows go across, and columns go downwards, assuming the indices are increasing.

This is very important. Because x and y are essentially indices to a specific location in the grid.

Although that is probably the cause, you also have a few semantic errors that I'll fix below.

BufferedReader input = new BufferedReader(new FileReader("maze.txt")); //Read the first two lines of the text file and determine the x and y length of the 2d array int mazeWidth = Integer.parseInt(input.readLine()); //A.K.A number of columns int mazeHeight= Integer.parseInt(input.readLine()); //A.K.A number of rows // ranges: [0...3][0...5] in our example mazeArray = new char[mazeWidth][mazeHeight]; char [] line; //Add each line of input to mazeArray for (int y = 0; y < mazeHeight; y++) { line = (input.readLine()).toCharArray(); if ( line.length != mazeWidth ) { System.err.println("Error for line " + y + ". Has incorrect number of characters (" + line.length + " should be " + mazeWidth + ")."); } else { System.out.print(y + " : "); System.out.println(java.util.Arrays.toString(line)); mazeArray[y] = line; } } 

2 Comments

Thank you for this amazing explanation!
Ahh... I typed this up late in the night; forgive the weird sentence structures; I sometimes start writing like that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.