0

I want to read lines of numbers from a file. The code is as follows but the IDE shows NullPointerException runtime exception. Not sure what I am doing wrong.

//reading the contents of the file into an array
public static void readAndStoreNumbers() {
    //initialising the new object
    arr = new int[15][];

    try {
        //create file reader
        File f = new File("E:\\Eclipse Projects\\triangle.txt");
        BufferedReader br = new BufferedReader(new FileReader(f));

        //read from file
        String nums;
        int index = 0;
        while ((nums = br.readLine()) != null) {
            String[] numbers = nums.split(" ");

            //store the numbers into 'arr' after converting into integers
            for (int i = 0; i < arr[index].length; i++) {
                arr[index][i] = Integer.parseInt(numbers[i]);
            }
            index++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
2
  • which line showing nullpointerexception? Commented Jun 26, 2012 at 4:55
  • I would rather us a simple List of List, with index being the line number and value being the ints. Commented Jun 26, 2012 at 5:05

5 Answers 5

5

Your second dimension of arr is uninitialized, and you are invoking

arr[index].length
Sign up to request clarification or add additional context in comments.

Comments

1

You could be running into an NPEX for two reasons.

  1. You don't finish your definition of arr - it's not evident in your code that you declare arr as int arr[][];

  2. Even if you had the above, you wouldn't have set aside space for your second array. What you have now is a jagged array; you can have elements of whatever length in the second dimension you wish in your second array.

    The only modification I made to your code to get it to work would be the following line:

    arr[index] = new int[numbers.length];
    

    ...after pulling elements into numbers, and before entering the loop.

Comments

0

you need to change -

for(int i=0; i<arr[index].length; i++) {

to

arr[index] = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {

Comments

0

Java doesn't have real multidimensional arrays. What you are using is actually an array of int arrays: new int[n][] actually creates an array with room for n objects of type int[].

Consequently you will have to initialize each of those int arrays separately. That would have been obvious from the fact that you never actually specified the length of the second dimension anywhere in your program.

Comments

0

I think you should use StringBuilder..

//reading the contents of the file into an array
public static void readAndStoreNumbers() {
    //initialising the StringBuffer 
    StringBuilder sb = new StringBuilder();

    try {
        //create file reader
        File f = new File("E:\\Eclipse Projects\\triangle.txt");
        BufferedReader br = new BufferedReader(new FileReader(f));

        //read from file
        String nums;
        int index = 0;
        while ((nums = br.readLine()) != null) {
            String[] numbers = nums.split(" ");

            //store the numbers into 'arr' after converting into integers
            for (int i = 0; i < arr[index].length; i++) {
                sb.append(Integer.parseInt(numbers[i])).append("\n");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Comments

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.