Bug 53613

Summary: org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Rule M2.4 exception : this error should NEVER happen
Product: POI Reporter: James Batchelor <jbatchelor>
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED DUPLICATE    
Severity: normal CC: andreas, gouriajit.gadre, mustangxu, wisemaj2
Priority: P2    
Version: 3.8-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: This is the spreadsheet that my program tries to open and close. The program corrupts the spreadsheet when run.

Description James Batchelor 2012-07-27 14:42:05 UTC
Created attachment 29122 [details]
This is the spreadsheet that my program tries to open and close.  The program corrupts the spreadsheet when run.

I experience this exception being thrown:

org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Rule M2.4 exception : this error should NEVER happen, if so please send a mail to the developers team, thanks !

This is example code that triggers the error.  The statement wb.write(fileOut); specifically seems to be the culprit.

package areacontrol;

public class AreaControl_3 {
    
    public static void main(String[] args) {
      String filename = "C:\\java\\values.xlsx";
      
      try{
          org.apache.poi.openxml4j.opc.OPCPackage opc = 
                  org.apache.poi.openxml4j.opc.OPCPackage.open(filename);
          
          org.apache.poi.xssf.usermodel.XSSFWorkbook wb = 
                  new org.apache.poi.xssf.usermodel.XSSFWorkbook(opc);
          
          opc.close();
          
          java.io.FileOutputStream fileOut = 
                  new java.io.FileOutputStream(filename);
          
          wb.write(fileOut);
          
          fileOut.close();  
          
        }catch(Exception e){
            System.out.println(e);
        }
   }
}

This code does not generate the exception:

package areacontrol;

public class AreaControl_3_1 {
    
    public static void main(String[] args) {
      String filename = "C:\\java\\values.xlsx";
      
      try{          
          java.io.FileInputStream fis = 
                  new java.io.FileInputStream(filename);
          
          org.apache.poi.xssf.usermodel.XSSFWorkbook wb = 
                  new org.apache.poi.xssf.usermodel.XSSFWorkbook(fis);
          
          fis.close();
                    
          java.io.FileOutputStream fileOut = 
                  new java.io.FileOutputStream(filename);
          
          wb.write(fileOut);
          
          fileOut.close();                    
          
        }catch(Exception e){
            System.out.println(e);
        }
   }
}

I have attached the spreadsheet.
Comment 1 Mark B 2012-07-29 11:04:40 UTC
Not so sure that this is an error but it certainly could be a problem with the javadoc for the XSSFWorkbook/OPCPackage classes.

Had a quick play with the code this morning and read the full stacktrace produced when the exception is thrown. The source of the exception is the write method of the workbook - or one of the methods it calls - and it appears to be caused by the fact that the OPCPackage is no longer available when the workbook is being written out. If the call to the close() method of the OPCPackage instance if moved to follow the write() method of the workbook, then the exception is not thrown.

So, it seems that if a workbook is cretade from an instance of the OPCPackage class, the OPCPacakge instance must not be disposed of until after the workbook has been written. This change the the code, removed the problem completely;

org.apache.poi.openxml4j.opc.OPCPackage opc = 
   org.apache.poi.openxml4j.opc.OPCPackage.open(filename);
org.apache.poi.xssf.usermodel.XSSFWorkbook wb =
   new org.apache.poi.xssf.usermodel.XSSFWorkbook(opc);
java.io.FileOutputStream fileOut = new java.io.FileOutputStream(filename);
wb.write(fileOut);
opc.close();
fileOut.close();  

Perhaps documenting this in the javadoc for the workbook and OPCPackage classes would be sufficient.

Mark B
Comment 2 Dominik Stadler 2016-07-26 19:38:10 UTC
In Bug #59634 we adjusted javadoc to state that the object should not be used at all any more after the close() method was called. See also r1753912.

*** This bug has been marked as a duplicate of bug 59634 ***