1

I tried to do an excel data upload into a database using Apache.POI. their function gives me an error like

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.apache.poi.xssf.usermodel.XSSFCell cannot be cast to java.lang.Integer

The code segment error occurs in

for (Iterator iterator = dataHolder.iterator(); iterator.hasNext();) {
        List list = (List) iterator.next();
         i++;
        if (i > 0) {  
            ID =  (int) list.get(0);
            Employee_Number = list.get(1).toString();
            FirstName = list.get(2).toString();
            LastName = list.get(3).toString();
            EmailAddress = list.get(4).toString();
            PdfName = list.get(5).toString();
            Sup_EmailAddress = list.get(6).toString();
            PassCode = list.get(7).toString();

can anyone suggest me why getting this kind of error?.

2 Answers 2

3

According to the official JavaDoc of XSSFCell you can use getNumericCellValue() instead.

public double getNumericCellValue()

Get the value of the cell as a number.

To safely check whether you process an actual number, you can re-write your code fragment as follows:

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;

int ID;
// ... loop here ...
    XSSFCell cell = list.get(0);
    if(CellType.NUMERIC == cell.getCellType()) {
        ID =  new Double(cell.getNumericCellValue()).intValue(); 
    } else {
        // handle this case separately... 
    }
// end loop

The above code snippet avoids unnecessary casts and/or try and error guessing.

Hope it helps.

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

3 Comments

You shold mention in what apache poi version XSSFCell.getCellType returns a CellType. In latest stable release Apache POI 3.17 XSSFCell.getCellType returns an int. There XSSFCell.getCellTypeEnum is needed to get a CellType.
That's a useful hint. I relied on the latest public JavaDoc as linked in the answer. In this version, getCellType() is not marked as deprecated, however, getCellTypeEnum() is marked as deprecated. Quite confusing, isn't it? The linked JavaDoc version states "Deprecated. use getCellType instead".
Yes this is confusing. The apache poi programmers are updating the latest public JavaDoc always to be the JavaDoc for the currently nightly build instead of the latest stable version. Many users are confused about this. And professional users will definitely not using any nightly build in productive usage. So always own JavaDocs are necessary from downloaded source.
0

The line ID = (int) list.get(0); throws an error because the data in the cell cannot be converted to an int. Try to save it as a String.

id = (String) list.get(0);

Or as a long

id = (long) list.get(0);

Or handle the exception

try {
  ID = (int) list.get(0);
} catch (ClassCastException e) {
  // id is not a number
}

2 Comments

are not there any other way to keep it as a int value?
You cannot save a value as a number if it's not a number. But you can skip it if you don't need it by handling the exception.

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.