NullPointerException is thrown when IfCommand condition is false

Issue #53 resolved
Tomoyuki Tanaka
created an issue

In 2.3.0.

I used "If-Command" without "elseArea".
If condition was false, a NullPointerException was thrown at XlsArea.

Log is following.

[ERROR] 2016-07-04 23:23:53,416 o.j.a.XlsArea   - Failed to update row height for src row=-1 and target row=-1 
java.lang.NullPointerException: null
    at org.jxls.area.XlsArea.updateRowHeights( [jxls-2.3.0.jar:na]
    at org.jxls.area.XlsArea.transformStaticCells( [jxls-2.3.0.jar:na]
    at org.jxls.area.XlsArea.applyAt( [jxls-2.3.0.jar:na]
    at org.jxls.command.IfCommand.applyAt( [jxls-2.3.0.jar:na]
    at org.jxls.area.XlsArea.applyAt( [jxls-2.3.0.jar:na]
    at org.jxls.util.JxlsHelper.processTemplate( [jxls-2.3.0.jar:na]
    at org.jxls.util.JxlsHelper.processTemplate( [jxls-2.3.0.jar:na]
    at sample.jxls.Main.main( [main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
    at sun.reflect.NativeMethodAccessorImpl.invoke( ~[na:1.8.0_92]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[na:1.8.0_92]
    at java.lang.reflect.Method.invoke( ~[na:1.8.0_92]
    at com.intellij.rt.execution.application.AppMain.main( [idea_rt.jar:na]


    private void updateRowHeights(CellRef areaStartCellRef, int relativeStartRow, int relativeEndRow) {
        for (int srcRow = relativeStartRow; srcRow <= relativeEndRow; srcRow++) {
            if (!cellRange.containsCommandsInRow(srcRow)) {
//                CellRef relativeCell = cellRange.getCell(srcRow, 0);
                int maxRow = cellRange.findTargetRow(srcRow);
                int targetRow = areaStartCellRef.getRow() + maxRow;
                try {
                    transformer.updateRowHeight(startCellRef.getSheetName(), srcRow, areaStartCellRef.getSheetName(), targetRow);
            //      ^^^^^^^^^^^
            //      this transformer is null!!
                } catch (Exception e) {
                    logger.error("Failed to update row height for src row={} and target row={} ", srcRow, targetRow, e);

If using "If-Command" without "elseArea", elseArea of IfCommand is used XlsArea.EMPTY_AREA.
But transformer of XlsArea.EMPTY_AREA is null.
So NullPointerException is thrown at updateRowHeights() method.

I guess that null check is needed before transformer.updateRowHeight().

Thank you.

Comments (9)

  1. Roberto Sanchez

    @Tomoyuki Tanaka Perhaps I am missing something here, but in template.xlsx attached to this issue, the command is:

    jx:if(condition="false" lastCell="A1")

    It looks like the actual problem is that there is no area specified at all, not even an IfArea. That is, the transformer would have to be null because no area references were found when processing the command. If you change it to this, does it work?

    jx:if(condition="false" lastCell="A1" areas=["A1:A1"])

    If so, I think that the right solution to this issue is to check for that specific condition and emit a more sensible error message when throwing the exception.

  2. Tomoyuki Tanaka reporter

    I'm sorry for the late reply.

    Java code is following.

    import org.jxls.common.Context;
    import org.jxls.util.JxlsHelper;
    public class Main {
        public static void main(String[] args) throws Exception {
            try (InputStream in = new FileInputStream(new File("./template.xlsx"));
                 OutputStream out = new FileOutputStream(new File("./out.xlsx"))) {
                Context context = new Context();
                JxlsHelper.getInstance().processTemplate(in, out, context);
  3. Log in to comment