-1

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.

  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – itwasntme Jan 19 '20 at 04:13
  • 1
    Hey, please mark which line is line 41 in Weather2.java in your posted code. Actually better if you copy paste that line seperately explaining that is where the error is. – CausingUnderflowsEverywhere Jan 19 '20 at 04:43
  • Post has been edited – John Gunasar Jan 19 '20 at 05:03
  • 1
    Did you copy paste this code from an example because you have two pieces of code that seem to do the same thing there, and it appears you lack understanding of how it works. – CausingUnderflowsEverywhere Jan 19 '20 at 05:58
  • while using the equals method in your code I suggest to use it in the following manner `"NUMERIC".equals(nextRow.getCell(i+1).getCellType())` this would save you from the potential null pointer exceptions – ThatMan Jan 19 '20 at 06:06

1 Answers1

1

The null pointer exception may be happening in one of two statements.

if(nextRow.getCell(i+1).getCellType().equals("NUMERIC") && nextRow.getCell(i).toString().length()==5)

Either in this one: nextRow.getCell(i+1).getCellType().equals("NUMERIC") Or this one: nextRow.getCell(i).toString().length()==5

You must be supplying an index with no cell.

I suggest you ditch your code 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()); }

And use this code instead because it won't be able to generate a null pointer exception. (If you aren't expecting any null cells.) while(cellIterator.hasNext()) { Cell cell = cellIterator.next(); }

Otherwise if you want to continue using the previous code, you need to expand the code into multiple lines and use null checks. eg.

Cell cell = nextRow.getCell(i+1);
if (cell != null) {
//do numeric and length check here
}

Verify your usage of nextRow.getCell(i+1) and nextRow.getCell(i) note in the first you're using i + 1 and in the next just i.