0

I've been getting null pointer exception while reading a file into an array. i realized that the exception appears when it is null and something else is required. The array minefield has already been initialized. The exception happens on " minefield[i][j]=input.charAt(j)+"";"

I'm trying to read in a file with this format:

#of row
#of column
abcd
efgh
ijkl

This is the code:

     try {
            BufferedReader in =new BufferedReader (new FileReader(name+".txt"));
            String input=in.readLine();      
            row = Integer.parseInt(input);
            input=in.readLine();
            col = Integer.parseInt(input);
            int c =0;
            input=in.readLine();
            for (int i=0;i<row;i++){
            input=in.readLine();
            for (int j=0;j<col;j++){
                  System.out.println (input.charAt(j));
                  minefield[i][j]=input.charAt(j)+"";
               }
            }
            System.out.println("The file has been loaded");
            in.close();
         }
            catch(IOException iox){
               System.out.println ("Error reading file");
            }

Your help is greatly appreciated. Edit: Sorry i left something out.

4
  • Which are your values for i and j? Commented Sep 19, 2012 at 0:46
  • What is row? Are there row lines in the file? Commented Sep 19, 2012 at 0:46
  • Show us the stack trace. It will have the exact line number where the exception occurred. Commented Sep 19, 2012 at 0:51
  • I've added the missing information. The exception happens "minefield[i][j]=input.charAt(j)+"";" Commented Sep 19, 2012 at 0:52

4 Answers 4

1

Method readLine() returns null when the end of the stream is reached, but you don't check it.

See http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#readLine--

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

Comments

0

Check that minefield has been initialized.

Simply declaring minefield is not enough as it default to null.

private String[][] minefield;

You need to assign a new String[][] array to it.

private String[][] minefield = new String[4][5];

Comments

0

Given the contents of your input file, you have an extra input = in.readLine(); that doesn't have a matching line entry:

...
int c =0;
input=in.readLine();   <-- remove this line
for (int i=0;i<row;i++){
...

Comments

0

Make sure that name and minefield are initialized. Then, I would rewrite your code to safeguard against a mismatch between whatever value you have in row and the number of lines in the file:

try {
    BufferedReader in =new BufferedReader (new FileReader(name+".txt"));
    String input;
    for (int i=0;i<row;i++){
        input=in.readLine();
        if (input == null) {
           throw new IOException("Expected " + row +
               " lines in the file; only found " + i + " lines");
        }
        for (int j=0;j<col;j++){
            System.out.println (input.charAt(j));
            minefield[i][j]=input.charAt(j)+"";
        }
    }
    System.out.println("The file has been loaded");
    in.close();
 }
 catch(IOException iox){
     System.out.println ("Error reading file");
 }

EDIT Now that you've updated your question with more complete code, it's clear what the problem is. After you read the number of rows and columns from the file, you are reading one line for each row, but you are reading one additional line before you start the loop for the number of rows. Unless the file contains a blank line, this is causing you to overrun the end of the file.

3 Comments

This would still give me the exception at "minefield[i][j]=input.charAt(j)+""; "
@user1337628 - Then minefield must not be initialized. With the check for null before the j loop, there's no way for input to be null there, and minefield is the only other source of a NPE.
Ah, i forgot to reinitialize minefield Thanks for you help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.