1. leonate NA
  2. jxls
  3. Issues


Issue #53 resolved

NullPointerException is thrown when IfCommand condition is false

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(XlsArea.java:400) [jxls-2.3.0.jar:na]
    at org.jxls.area.XlsArea.transformStaticCells(XlsArea.java:469) [jxls-2.3.0.jar:na]
    at org.jxls.area.XlsArea.applyAt(XlsArea.java:224) [jxls-2.3.0.jar:na]
    at org.jxls.command.IfCommand.applyAt(IfCommand.java:97) [jxls-2.3.0.jar:na]
    at org.jxls.area.XlsArea.applyAt(XlsArea.java:168) [jxls-2.3.0.jar:na]
    at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:102) [jxls-2.3.0.jar:na]
    at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:94) [jxls-2.3.0.jar:na]
    at sample.jxls.Main.main(Main.java:20) [main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [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;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    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