cyberangles blog

How to Fix 'CELL_TYPE_STRING cannot be resolved or is not a field' Error in Apache POI Java Code

Apache POI is a popular Java library used for reading and writing Microsoft Office files, including Excel (XLS and XLSX). It simplifies tasks like parsing spreadsheets, extracting data, and creating reports. However, like any library, POI evolves, and deprecated features are removed in newer versions. One common error developers encounter when upgrading POI is:

CELL_TYPE_STRING cannot be resolved or is not a field

This error occurs when old, deprecated constants for cell types are used with newer POI versions. In this blog, we’ll demystify why this error happens, walk through step-by-step fixes, and provide examples to ensure your code works seamlessly with modern POI releases.

2025-12

Table of Contents#

  1. Understanding the Error
  2. Why the Error Occurs: POI Version Changes
  3. Step-by-Step Fixes
  4. Common Pitfalls and How to Avoid Them
  5. Example Code: Before and After the Fix
  6. Conclusion
  7. References

Understanding the Error#

The error message CELL_TYPE_STRING cannot be resolved or is not a field indicates that the Java compiler cannot find the constant CELL_TYPE_STRING in the class where it’s referenced (typically HSSFCell or XSSFCell). This happens because:

  • Deprecated Constants: Prior to Apache POI 4.x, cell types (e.g., string, numeric, boolean) were defined as integer constants in classes like HSSFCell (for .xls files) and XSSFCell (for .xlsx files). Examples include CELL_TYPE_STRING, CELL_TYPE_NUMERIC, and CELL_TYPE_BOOLEAN.
  • Removed in Newer Versions: In POI 4.0.0 and later, these integer constants were deprecated and eventually removed. They were replaced with a type-safe CellType enum to improve code readability and maintainability.

Why the Error Occurs: POI Version Changes#

To resolve the error, it’s critical to understand how POI’s cell type handling evolved:

Pre-POI 4.x (e.g., 3.17 and earlier):#

Cell types were represented as integer constants in HSSFCell/XSSFCell. For example:

// Old code (pre-4.x)  
int cellType = cell.getCellType(); // Returns an int (e.g., 1 for STRING)  
if (cellType == HSSFCell.CELL_TYPE_STRING) {  
    String value = cell.getStringCellValue();  
}  

POI 4.x:#

The integer constants were deprecated, and a CellType enum was introduced. The method getCellType() was deprecated in favor of getCellTypeEnum(), which returns a CellType enum:

// POI 4.x code  
CellType cellType = cell.getCellTypeEnum(); // Returns CellType enum  
if (cellType == CellType.STRING) {  
    String value = cell.getStringCellValue();  
}  

POI 5.x and Later:#

getCellTypeEnum() was deprecated, and getCellType() was reintroduced to return the CellType enum directly (simplifying the API):

// POI 5.x+ code  
CellType cellType = cell.getCellType(); // Returns CellType enum (no "Enum" suffix)  
if (cellType == CellType.STRING) {  
    String value = cell.getStringCellValue();  
}  

Key Takeaway:#

If you’re using POI 4.x or later, the old CELL_TYPE_* constants no longer exist. Your code must use the CellType enum instead.

Step-by-Step Fixes#

Follow these steps to resolve the error:

Step 1: Identify Your Apache POI Version#

First, confirm which POI version your project uses. This determines how you’ll update the code.

  • Maven: Check pom.xml:

    <dependency>  
        <groupId>org.apache.poi</groupId>  
        <artifactId>poi</artifactId>  
        <version>5.2.3</version> <!-- Your version here -->  
    </dependency>  
    <dependency>  
        <groupId>org.apache.poi</groupId>  
        <artifactId>poi-ooxml</artifactId>  
        <version>5.2.3</version> <!-- Match POI version -->  
    </dependency>  
  • Gradle: Check build.gradle:

    implementation 'org.apache.poi:poi:5.2.3'  
    implementation 'org.apache.poi:poi-ooxml:5.2.3'  
  • Manual JARs: Check the filename (e.g., poi-5.2.3.jar).

Step 2: Replace Deprecated Constants with CellType Enum#

Update all references to CELL_TYPE_* constants with the corresponding CellType enum values.

Old Constant (Pre-4.x)New CellType Enum (4.x+)
CELL_TYPE_STRINGCellType.STRING
CELL_TYPE_NUMERICCellType.NUMERIC
CELL_TYPE_BOOLEANCellType.BOOLEAN
CELL_TYPE_FORMULACellType.FORMULA
CELL_TYPE_BLANKCellType.BLANK
CELL_TYPE_ERRORCellType.ERROR

Step 3: Use the Correct Method to Get Cell Type#

Depending on your POI version, use the appropriate method to retrieve the cell type:

POI VersionMethod to Get Cell TypeReturns
4.xcell.getCellTypeEnum()CellType enum
5.x+cell.getCellType()CellType enum

Example Fix Workflow#

Suppose you have old code that throws the error:

// Old code (causes "CELL_TYPE_STRING cannot be resolved" error in POI 4.x+)  
import org.apache.poi.hssf.usermodel.HSSFCell;  
 
// ...  
 
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { // ERROR HERE  
    String value = cell.getStringCellValue();  
}  

Fix for POI 4.x:

// Fixed code for POI 4.x  
import org.apache.poi.ss.usermodel.Cell;  
import org.apache.poi.ss.usermodel.CellType;  
 
// ...  
 
CellType cellType = cell.getCellTypeEnum(); // Use getCellTypeEnum()  
if (cellType == CellType.STRING) { // Use CellType.STRING enum  
    String value = cell.getStringCellValue();  
}  

Fix for POI 5.x+:

// Fixed code for POI 5.x+  
import org.apache.poi.ss.usermodel.Cell;  
import org.apache.poi.ss.usermodel.CellType;  
 
// ...  
 
CellType cellType = cell.getCellType(); // Use getCellType() (no "Enum" suffix)  
if (cellType == CellType.STRING) { // Use CellType.STRING enum  
    String value = cell.getStringCellValue();  
}  

Common Pitfalls and How to Avoid Them#

1. Forgetting to Update All CELL_TYPE_* Instances#

The error often occurs when only some constants are updated. Search your codebase for all instances of CELL_TYPE_ (e.g., CELL_TYPE_NUMERIC, CELL_TYPE_BOOLEAN) and replace them with CellType enum values.

2. Mixing getCellType() and getCellTypeEnum()#

Using getCellType() in POI 4.x will return a deprecated int (not the enum), leading to errors. Always use getCellTypeEnum() for POI 4.x and getCellType() for 5.x+.

3. Null Cell Handling#

If a cell is null (e.g., an empty cell in Excel), calling getCellType() or getCellTypeEnum() will throw a NullPointerException. Check for null first:

if (cell != null) { // Check if cell exists  
    CellType cellType = cell.getCellType(); // POI 5.x+  
    if (cellType == CellType.STRING) {  
        // ...  
    }  
}  

4. Using HSSFCell/XSSFCell Instead of the Cell Interface#

The CellType enum is defined in the Cell interface (common to all Excel formats like .xls and .xlsx). Use the Cell interface instead of concrete classes like HSSFCell or XSSFCell for portability:

// Good: Use the Cell interface  
Cell cell = row.getCell(0);  
 
// Avoid: Tight coupling to HSSFCell/XSSFCell  
HSSFCell hssfCell = (HSSFCell) row.getCell(0); // Unnecessary  

Example Code: Before and After the Fix#

Problematic Code (Causes Error)#

This code uses deprecated CELL_TYPE_STRING and will fail in POI 4.x+:

import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.apache.poi.ss.usermodel.Row;  
import org.apache.poi.ss.usermodel.Sheet;  
import org.apache.poi.ss.usermodel.Workbook;  
 
import java.io.FileInputStream;  
import java.io.IOException;  
 
public class ExcelReader {  
    public static void main(String[] args) {  
        try (Workbook workbook = new HSSFWorkbook(new FileInputStream("data.xls"))) {  
            Sheet sheet = workbook.getSheetAt(0);  
            for (Row row : sheet) {  
                for (org.apache.poi.ss.usermodel.Cell cell : row) {  
                    // ERROR: HSSFCell.CELL_TYPE_STRING cannot be resolved  
                    if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {  
                        System.out.println("String value: " + cell.getStringCellValue());  
                    }  
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  

Fixed Code (POI 5.x+)#

This code uses CellType.STRING and getCellType(), resolving the error:

import org.apache.poi.ss.usermodel.Cell;  
import org.apache.poi.ss.usermodel.CellType;  
import org.apache.poi.ss.usermodel.Row;  
import org.apache.poi.ss.usermodel.Sheet;  
import org.apache.poi.ss.usermodel.Workbook;  
import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
 
import java.io.FileInputStream;  
import java.io.IOException;  
 
public class ExcelReader {  
    public static void main(String[] args) {  
        try (Workbook workbook = new XSSFWorkbook(new FileInputStream("data.xlsx"))) {  
            Sheet sheet = workbook.getSheetAt(0);  
            for (Row row : sheet) {  
                for (Cell cell : row) {  
                    if (cell != null) { // Check for null cells  
                        CellType cellType = cell.getCellType(); // POI 5.x+: getCellType() returns enum  
                        if (cellType == CellType.STRING) { // Use CellType.STRING enum  
                            System.out.println("String value: " + cell.getStringCellValue());  
                        }  
                    }  
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  

Conclusion#

The CELL_TYPE_STRING cannot be resolved error is caused by using deprecated integer constants with Apache POI 4.x or later. The solution is to:

  1. Check your POI version (4.x vs. 5.x+).
  2. Replace CELL_TYPE_* constants with the CellType enum (e.g., CellType.STRING).
  3. Use the correct method to get the cell type: getCellTypeEnum() for 4.x, getCellType() for 5.x+.

By following these steps, you’ll resolve the error and ensure your code is compatible with modern POI releases. Always refer to the official POI documentation for the latest API changes!

References#