I am reading an excel file: https://drive.google.com/drive/u/0/folders/0AIRmPm_DPjucUk9PVA
For now, all I want to do is print to the console each entry in this excel file in the format of: zip code - city - state. For now, all I am trying to do is get zip codes. Here is my approach:
FileInputStream inputStream = new FileInputStream("C:/Users/student/eclipse-workspace/Weather2/bin/resources/uszips.xlsx");
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
int i = 0;
while(iterator.hasNext()) {
Row nextRow = iterator.next();
if(nextRow.getCell(i+1).getCellType().equals("NUMERIC") && nextRow.getCell(i).toString().length()==5) //this if statement is where the error occurs {
System.out.println(nextRow.getCell(i).getNumericCellValue());
}
Iterator<Cell> cellIterator = nextRow.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
}
i++;
}
workbook.close();
inputStream.close();
}
}
I get the following error when I run this method:
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.NullPointerException
at Forecast.search_Zipcodes(Weather2.java:41)
at Weather2.main(Weather2.java:94)
... 11 more
Exception running application Weather2
I don't understand why I receive a null pointer exception. I know for sure that a value for the cell exists, or is the null pointer exception for something else? Any help would be appreciated, thanks.