#2 Operand Stack: NullPointerException for try-block

BCV 4.2.1
closed
None
2013-06-02
2013-05-09
Duc Thanh Nguyen
No

The try-finally block without catch causes a NullPointerException in the OperandStack class. Hier is a SourceCode sample:

package test;

public class CV {
    static int m() {
    int i = 0; 
    try {
        i++;
    } finally {
                i--;
        }
    return i;
   }
}

If the corresponding byte code of the method m() is selected in the operand stack view the NullPointerException is thrown.

java.lang.NullPointerException
    at com.drgarbage.bytecode.BytecodeUtils.resolveConstantPoolTypeName(BytecodeUtils.java:398)
    at com.drgarbage.bytecodevisualizer.operandstack.OperandStack.getConstantPoolClassName(OperandStack.java:1151)
    at com.drgarbage.bytecodevisualizer.operandstack.OperandStack.getStackBefore(OperandStack.java:399)
    at com.drgarbage.bytecodevisualizer.operandstack.OperandStack.calculateOperandStack(OperandStack.java:347)
    at com.drgarbage.bytecodevisualizer.operandstack.OperandStack.parseGraph(OperandStack.java:307)
    at com.drgarbage.bytecodevisualizer.operandstack.OperandStack.generateOperandStack(OperandStack.java:238)
    at com.drgarbage.bytecodevisualizer.operandstack.OperandStack.<init>(OperandStack.java:202)
    at com.drgarbage.bytecodevisualizer.view.OperandStackViewPage.setInput(OperandStackViewPage.java:1182)
    at com.drgarbage.bytecodevisualizer.view.OperandStackViewPage.setInput(OperandStackViewPage.java:704)
    at com.drgarbage.bytecodevisualizer.editors.BytecodeEditor.doHandleCursorPositionChanged(BytecodeEditor.java:1312)
    at com.drgarbage.bytecodevisualizer.editors.BytecodeEditor.handleCursorPositionChanged(BytecodeEditor.java:1779)
    at org.eclipse.ui.texteditor.AbstractTextEditor$18.mouseUp(AbstractTextEditor.java:3178)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:220)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4136)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1458)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1481)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1466)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1271)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3982)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3621)

Discussion

  • For the java source code above the following byte code is generated:

    static int m() {
            0 iconst_0;
            1 istore_0;          /* i */
            try {
                2 iinc 0 1;      /* i++ */
                5 goto 9;
            }
            finally {            /* covers byte codes 2 to 8 */
                8 astore_1;
                9 iinc 0 -1;     /* i-- */
                12 aload_1;
                13 athrow;
            }
            14 iinc 0 -1;        /* i-- */
            17 iload_0;          /* i */
            18 ireturn;
    }
    

    The exception table of this method includes following entries:

    00 01             /* u2 exception_table_length=1 */
    00 02 00 08       /* u2 start_pc=2 u2 end_pc=8 */
    00 08 00 00       /* u2 handler_pc=8 u2 catch_type=0 */
    

    The attribute catch_type=0 is an index in a constant pool of an exception.
    The constant pool contains no index 0.

    This is actually a compiler error. However the error is insignificant, because the byte code instruction

    8 astore_1;
    

    will store any exception from the operand stack and JVM doesn't need to know what kind of exception has been thrown.

     
    Last edit: Sergej Alekseev 2013-05-23
  • fixed in revision 186