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();
    }
}
Community
  • 1
  • 1
h-rai
  • 3,636
  • 6
  • 52
  • 76

5 Answers5

5

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

arr[index].length
jmj
  • 237,923
  • 42
  • 401
  • 438
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.

Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230
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();
    }
}
swpalmer
  • 3,890
  • 2
  • 23
  • 31
RTA
  • 1,251
  • 2
  • 14
  • 33
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++) {
Kshitij
  • 8,474
  • 2
  • 26
  • 34
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.

Wormbo
  • 4,978
  • 2
  • 21
  • 41