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)
int [][] numbers = new int [array.length][];... the second dimension isn't specified.array[row]isnullfor some row.citiesvariable before passing it to the converter. Or even better, debug.