0

I'm trying to read some cells in a line, first 5 sheets using the same reading code were ok, but my 6th sheet is returning a null value in a specific cell even when the cell has value in the spreadsheet.

Has anyone seen this one before? How to solve it?

Thanks in advance!

PS.: here's the code I'm using to get the values, works fine 'til it reaches the 6th sheet. I can see that the Excel spreadsheet is filled but still getting a null.

private String getValorTexto(HSSFSheet sheet, int indiceLinha, int indiceColuna) 
{              
    sheet.getRow(indiceLinha).getCell(indiceColuna).setCellType(HSSFCell.CELL_TYPE_STRING);
    if (sheet.getRow(indiceLinha).getCell(indiceColuna).getCellType() == HSSFCell.CELL_TYPE_STRING) {
        return sheet.getRow(indiceLinha).getCell(indiceColuna).getStringCellValue().trim();
    } else {
        return String.valueOf(sheet.getRow(indiceLinha).getCell(indiceColuna).getNumericCellValue());
    }
}
4
  • 2
    Please post the exception message and stack trace, and indicate on which line the error has occurred. What are the values of indiceLinha and indiceColuna? Indexes into rows and columns are 0-based in Apache POI, so that "A1" corresponds to (0, 0), not (1, 1). Commented Apr 24, 2014 at 21:56
  • indiceLinha is the row number, I'm iterating through the rows. indiceColuna is the column number, I'm iterating over them too. The problem is that even though this particular cell has a value, getValorTexto is returning null. The other cells are fine, I'm able to retrieve their value properly. Commented Apr 25, 2014 at 1:52
  • Share the exception log... Continuing the Rgettman's comment, this is also notable that the sheet, rows, columns etc are zero index based. i.e. starts with zero, not one. so take care of that too. Commented Apr 25, 2014 at 5:26
  • Here's the deal, I'm not getting an exception, everything works fine, I'm just not getting the result from that particular cell of the spreadsheet. The indexes are fine, starting from zero. The content of this cell is '303249833' but I'm getting null every time I try to read this cell. It's a very weird problem. Commented Apr 25, 2014 at 12:21

2 Answers 2

2

I guess that forcing cell type to HSSFCell.CELL_TYPE_STRING might be the reason of getting null.

A more robust way to obtain a string value from any type of cell (you might also have dates, formulas, etc.) is to use DataFormatter:

import org.apache.poi.ss.usermodel.DataFormatter;

static DataFormatter dataFormatter = new DataFormatter();

static String getStringValue(Cell cell) {
    return dataFormatter.formatCellValue(cell);
}

It is especially useful when you want to get exactly the same what is displayed in Excel (e.g numbers rounded to given decimal places).

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

3 Comments

If this was the reason, then it must fire the exception on first five sheets as well. But as per the problem, its happening in 6th sheet not in first five sheet.
@Sankumarsingh Yes, you're right. Anyway I would suggest to try the way with DataFormatter.
@LukaszWiktor, it worked perfectly. I totally agree with Sankumarsingh, it should return null in the previous sheets but for some weird reason, it happened only in the 6th one. Anyway, I'm gonna replace my "getValorTexto" method body to use DataFormatter now. Thanks so much for all your help!!
0

I have found in recent tests, that the method DateUtil.GetJavaDate(Double value) can throw a NullReferenceException for unknown reasons. This is the method NPOI uses to convert the numeric Excel date when calling cell.DateCellValue(). I recommend instead to use the method DateTime.FromOADate(cell.NumericCellValue) which seem to do the same conversion.

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.