-1

I am getting a null pointer exception, but I dont know why. I checked to see if the cell was null before I read it to a string. So, why is that string null?

private void fillArray() 
{
    try 
    {
        readBook = new HSSFWorkbook(readFile);
    } 
    catch (IOException e) 
    {
        System.out.println("If we know what we're doing, no one should ever see this line.");
    }
    if (readBook != null) 
    {HSSFSheet infoSheet = readBook.getSheetAt(0);
        HSSFRow headingsRow = infoSheet.getRow(0);
        int i = 0;
        HSSFCell cell = headingsRow.getCell(i);
        String columnHeading = cell.toString();
        while (cell != null && !(cell.toString().equals(""))) 
        {
            cell = headingsRow.getCell(i);
            columnHeading = cell.toString();
            columnHeadings.add(columnHeading);
            i++;
        }
        if(columnListIsSetup == false)
        {
            createList();
            columnListIsSetup = true;
        }
    }
Chris Dargis
  • 5,203
  • 3
  • 33
  • 59
  • 7
    There are lots of places that *could* be throwing NullPointerException - what does the stack trace say? – Jon Skeet Jul 06 '12 at 21:10
  • It traces the exception to the line columnHeading = cell.toString(); – user1507835 Jul 06 '12 at 21:18
  • 2
    Then that shows that `cell` is null... or that it's being thrown within `toString`. – Jon Skeet Jul 06 '12 at 21:19
  • Yes, but the cell is NOT null, it will even println its contents, just not store it. – user1507835 Jul 06 '12 at 21:20
  • Then it must be the implementation of `toString` that's throwing the exception. (Except I suspect that actually `cell` *is* null, and you're just misdiagnosing it.) – Jon Skeet Jul 06 '12 at 21:21
  • OK, I changes .toString() to .getStringCellValue() and it still throws exception. Also, the cell deininetley is not null or else it would never execute the while loop. – user1507835 Jul 06 '12 at 21:24
  • Um, you're checking *before you assign a new value to `cell`*. There's nothing to say that `cell` won't be null after the first line of the body of the `while` loop. (Added an answer to that effect.) – Jon Skeet Jul 06 '12 at 21:29
  • cell is null. The first line in the while loop reassigns what null is, therefore, making the null check in the while loop condition fairly useless in this particular case. – Kevin Mangold Jul 06 '12 at 23:11
  • ^ i added an answer which could help you. – Kevin Mangold Jul 06 '12 at 23:22
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Krease Dec 15 '15 at 16:07

3 Answers3

5

I think this is the problem:

while (cell != null && !(cell.toString().equals(""))) 
{
    // We know that cell isn't null before this line...
    cell = headingsRow.getCell(i);

    // ... but now we've got a new value for cell, which could be null
    columnHeading = cell.toString();
    columnHeadings.add(columnHeading);
    i++;
}

I suspect you want to change it to:

while (cell != null && !(cell.toString().equals(""))) 
{
    // We know cell isn't null for this...
    columnHeading = cell.toString();
    columnHeadings.add(columnHeading);

    i++;
    // It's fine to set cell to null here... we'll be
    // checking again in a second...
    cell = headingsRow.getCell(i);
}
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
1
while (cell != null && !(cell.toString().equals("")))  {
    cell = headingsRow.getCell(i);      // here, cell gets reassigned so the 
                                        // "cell != null" check in the while 
                                        // loop condition loses it's value,
                                        // you need to check again

    if (cell == null)       // add the following to make sure the NEW cell value is not null
        break;              //

    columnHeading = cell.toString();
    columnHeadings.add(columnHeading);
    i++;
}
Kevin Mangold
  • 1,124
  • 8
  • 21
0

Before making sure cell is null (or) empty, you are doing String columnHeading = cell.toString(); which could be causing the NullPointerException in case where cell is null.

kosa
  • 63,683
  • 12
  • 118
  • 157
  • Thanks, I tried it, and still getting the exception. Its so weird because I was doing it randomly, and now every time I run it I get that exception, and nothing was changed. – user1507835 Jul 06 '12 at 21:15