0

I have file named input1.txt with contents as below:

a b c d

b d

c d

d e

I want to read it and put them in 2-dimensional array of Strings. I have written code for it. But it is showing NULL POINTER EXCEPTION. Where may be the error? Below is my code:

I am getting the exception in line graphNodes[i][j] = s;

BufferedReader br = null;
    BufferedReader cr = null;
    int lines = 0;
    try {
        br = new BufferedReader(new FileReader(filename));
        try {
            while (br.readLine() != null) {
                lines++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
    List<String> nodes = new ArrayList<String>();
    String[][] graphNodes = new String[lines][];
    String[] line = new String[lines];
    int i = 0;
    int j = 0, x = 0;
    try {
        cr = new BufferedReader(new FileReader(filename));
        while (cr.readLine() != null) {
            line[x] = cr.readLine();
            System.out.println("Line is: " + line[x]);
            String[] letters = line[x].split(" ");
            for (String s : letters) {
                 System.out.println("Letter is " + s);
                graphNodes[i][j] = s;
                j++;
            }
            i++;
            x++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
0

5 Answers 5

3

the graphNodes is missing column length

String[][] graphNodes = new String[lines][];

in your problem, once you get letters, you can initialize column of 2d array

String[] letters = line[x].split(" ");
graphNodes[i] = new String[letters.length];
0
1

You need to instantiate graphNodes[i] before accessing its j index.

2
  • To what value I need to instantiate? Commented Aug 18, 2013 at 7:58
  • It depends on the array length. If you don't know the length you can use an ArrayList instead. Commented Aug 18, 2013 at 8:00
1

I believe you're having issues with the following bit of code:

try {
    cr = new BufferedReader(new FileReader(filename));
    while (cr.readLine() != null) {
        line[x] = cr.readLine();
        System.out.println("Line is: " + line[x]);
        String[] letters = line[x].split(" ");
        for (String s : letters) {
             System.out.println("Letter is " + s);
            graphNodes[i][j] = s;
            j++;
        }
        i++;
        x++;
    }
}

This while statement says "while cr.readLine() != null" and at that very moment it read the first line of the file, compared it with null, and it wasn't null so it enters the loop. You then told it to set line[x] equal to cr.readLine() which it then reads the next line of the file, and sets it equal to line[x]. Thus skipping the first line of code doing nothing more with it than using it to check the while loop condition.

I think what you want in your while loop is something like this

try {
        cr = new BufferedReader(new FileReader(filename));
        for(String lineValue = cr.readLine(); lineValue != null; x++, lineValue = cr.readLine()) {
            line[x] = lineValue;
            System.out.println("Line is: " + line[x]);
            String[] letters = line[x].split(" ");
            for (String s : letters) {
                 System.out.println("Letter is " + s);
                graphNodes[i][j] = s;
                j++;
            }
            i++;
        }
    } 

But as someone mentioned previously, You need to declare the size of your 2 dimensional array. For the sake of this loop I just made it String[lines][100] but you'll want to adjust that to meet your needs (however long you anticipate your longest line of letters to be.

0
1

Well for one, you are not specifying a value for the second dimension: String[][] graphnodes = new String[lines][]. This means you are basically trying to set s to a value that cannot exist. you might try defining String[] letters first then doing something like String[][] graphnodes = new String[lines][letters.getLength()];

also, while (cr.readLine() != null) should be while (cr.hasNextLine())

your code would also look a lot cleaner if you did something like:

    for (int i = 0, j = 0, x = 0; cr.hasNextLine(); i++, x++) {
        line[x] = cr.readLine();
        System.out.println("Line is: " + line[x]);
        String[] letters = line[x].split(" ");
        for (String s : letters) {
             System.out.println("Letter is " + s);
            graphNodes[i][j] = s;
            j++;
        }
    }
0

First of All you don't need to read the files twice. Once to get the number of lines and the other to get the actual data. You need to read the lines and put them directly in a List. Then you can do whatever you need with that list.

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.