0

I am having an issue of verifying that the data from a csv file has been stored by a 2D array. When I try to display the array in my main, I get a Null Pointer Exception.

I have tried various youtube videos and looking on stackoverflow.

My add to 2D array code:

  public static void addTo2DArray(String[] tmpArray, int minCapacity) {
    int row = 0;
    int columns = 0;

    if ((minCapacity > row)) {
        row = (row * 3) / 2 + 1;
        String[][] newArray = new String[row][columns];
        for (int i = 0; i < crime2DArray.length; i++) {
            for (int j = 0; j < crime2DArray[i].length; j++) {
                newArray[i][j] = crime2DArray[i][j];
            }
        }
        crime2DArray = newArray;
    }
    crime2DArray[minCapacity - 1] = tmpArray;

}

Also my main code to display public class TestUSCrime {

public static void main(String[] args) {

    for (int row = 0; row < crime2DArray.length; row++) {
       for ( int column = 0 ; column <crime2DArray[row].length; column++) {
           System.out.print(crime2DArray[row][column] + " ");
       } 

       System.out.println();
    }
}

}

I am expecting to display the csv file in a 2D array but the output I am getting is the Null Pointer Exception that takes me back to line 15 of my main.

Line 15 is: for (int row = 0; row < crime2DArray.length; row++) {

Ras Zion
  • 41
  • 1
  • 1
  • 5
  • `crime2DArray` is most likely null at this point, which makes trying to access `crime2DArray.length` raise an NPE – Aaron Mar 28 '19 at 13:16

1 Answers1

0

You have NullPointerException on line 15 because crime2DArray is null. Execution of your program always start with main method. In your case in TestUSCrime class you have main method. Inside (the main method) you try directly to read the array.

To fix this you first have to get the data and put inside the array and then read it. I hope this helps.

Good luck to everyone!

Level_Up
  • 789
  • 5
  • 19
  • So I should put the part of my code that already reads the data from the csv inside my TestUSCrime? – Ras Zion Mar 28 '19 at 20:21
  • Yes try to put it before reading it in the main method. Or you can call your reading method there... – Level_Up Mar 28 '19 at 20:29