You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by Farley Lai <fa...@dynagrid.net> on 2016/08/14 04:37:38 UTC

NullPointerException thrown from an inner class method accessing a local variable

Hi all,

I need an anonymous class that extends an abstract class by implementing the
specified method​ just called in the constructor​.​ ​This ​​should be​
​quite common in Java but in Groovy, a NullPointerException is thrown when
access​ing​ a local variable.

Here is a minimal JUnit test showing this.

public class JavaTest {
    public abstract class Outer {
        public Outer(int i) {
            method(i+1);
        }
        public abstract void method(int i);
    }

    @Test
    public void testAnonymousClass() {
        final int local = 456;
        Outer o = new Outer(123) {
            @Override
            public void method(int i) {
                System.out.println(i);
                System.out.println(local);
            }
        };
    }
}

​When running ​in Java, 124 and the local variable 456 are print ​​without
problems.
However, in Groovy, a NullPointerException is thrown after 124 is print out.
I ran the test from IntelliJ and the Groovy version is 2.4.4.

Any ideas?

​Output as follows in Groovy:​

124

java.lang.NullPointerException
at edu.uiowa.gratis.data.tests.GroovyTest$1.method(GroovyTest.groovy:23)
at edu.uiowa.gratis.data.tests.GroovyTest$Outer.<init>(GroovyTest.groovy:11)
at edu.uiowa.gratis.data.tests.GroovyTest$1.<init>(GroovyTest.groovy)
at
edu.uiowa.gratis.data.tests.GroovyTest.testAnonymousClass(GroovyTest.groovy:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at​
​sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)



--
View this message in context: http://groovy.329449.n5.nabble.com/NullPointerException-thrown-from-an-inner-class-method-accessing-a-local-variable-tp5734631.html
Sent from the Groovy Users mailing list archive at Nabble.com.