Search code examples
javaapache-poixlsx

How to fix AbstractMethodError when uploading xlsx file through apache POI


I've been trying to make the upload (in an old web app) compatible for both xls and xlsx files, the xls is running fine, but uploading xlsx throws this error:

SEVERE: Servlet.service() for servlet [MainServlet] in context with path [/timesheet] threw exception [Servlet execution threw an exception] with root cause
java.lang.AbstractMethodError: org.apache.crimson.tree.ElementNode2.getTextContent()Ljava/lang/String;
    at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.readElement(PackagePropertiesUnmarshaller.java:146)
    at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.loadCreated(PackagePropertiesUnmarshaller.java:162)
    at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.unmarshall(PackagePropertiesUnmarshaller.java:124)
    at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:788)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:327)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:185)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:144)

current poi lib version is 3.14, but I've also tried 3.16 (same error) and 3.17, the app is currently using Java 1.6

public List<AttendanceRecord> getAttendanceRecords(List<AttendanceRecordErrMsg> errorMessages) throws Exception {

        Workbook wb = WorkbookFactory.create(new FileInputStream(arFile)); //app already throws an error upon reaching this line

        List<AttendanceRecord> attendanceRecordList = new ArrayList<AttendanceRecord>();


Solution

  • The reason for such error is because Crimson Library is included in your project, which only provide implementation (base on xml-apis 1.0.b2 as at latest version 1.1.3) on Node interface, and does not implement getTextContent() method that is introduced according to Document Object Model (DOM) Level 3 Core Specification.

    To fix such error, you can do one of the following:

    1.Remove Crimson Library from your project.

    2.Specify the javax.xml.parsers.DocumentBuilderFactory system property to avoid using org.apache.crimson.jaxp.DocumentBuilderFactoryImpl class by adding following line
    System.setProperty("javax.xml.parsers.DocumentBuilderFactory","com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");