0

I keep on getting a null pointer exception and I have no idea why. Explain please.

public static int[][] convertStringToInt(String[][] array){

    int [][] numbers = new int [array.length][];
    for(int row = 0; row < array.length; row++)
    {
        numbers[row] = new int [array[row].length];                //line 48
        for(int col = 0; col < array[row].length; col++)
        {
            numbers[row][col] = Integer.parseInt(array[row][col]);
        }
    }
    return numbers;
}



public static void main (String[] agrs)
{

    File selectedFile = selectFile("Enter fileName for double number, EX:    
                      decimalNumbers.csv");
    if( !selectedFile.exists())
    {
       System.out.print("\nFile does not exit, program terminating\n\n");
       System.exit(1);
    }

    int countLines = countLinesInFile(selectedFile);
    String cities [][] = loadArrayFromFile(selectedFile, countLines);

    int [][] unitsSold = convertStringToInt(cities); //line 157

    System.out.println(unitsSold);
 }

here are the errors when i enter the file name

Exception in thread "main" java.lang.NullPointerException
at labReview.ArrayOfArrays.convertStringToInt(ArrayOfArrays.java:48)
at labReview.ArrayOfArrays.main(ArrayOfArrays.java:157)
7
  • This works fine for me, can you give an input example where the error occurs? Commented Jan 30, 2014 at 6:25
  • I don't understand this: int [][] numbers = new int [array.length][]; ... the second dimension isn't specified. Commented Jan 30, 2014 at 6:26
  • It's not necessary to specify second dimension in 2D arrays. Try googling it. Commented Jan 30, 2014 at 6:27
  • 1
    That suggests that array[row] is null for some row. Commented Jan 30, 2014 at 6:28
  • Try printing your cities variable before passing it to the converter. Or even better, debug. Commented Jan 30, 2014 at 6:28

2 Answers 2

1

The method convertStringToInt works fine. The exception you described NullPointerException may occur in the line

numbers[row] = new int[array[row].length];

if array[row] is null. So the error may be occuring because the method loadArrayFromFile is returning a null row, like this (for example):

String[][] s = { { "1", "2", "3" }, { "1" }, null, { "3", "4" } };

Print the elements in the array returned by the method loadArrayFromFile to see if there are null rows.

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

Comments

0

I guess, you have a null array in your cities at some index.

String cities [][] = loadArrayFromFile(selectedFile, countLines);

Try printing your cities array as below

for(String[] cityArr : cities){
 System.out.println("Cities at Index "+i+" is : "+citiArr);
}

And if you see a "null" value at any index, then modify your loadArrayFromFile to replace a black array whenever you have a null while adding it to cities array.

....

if(citiArr==null){
 citiArr = new String[]{};
}
cities[i][j]=citiArr;

....

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.