today I wrote a small test class that shed some light on the unexplainable NullPointerException I was getting from some reflection code.
> /**
> * Little test class to test the behaviour of java using reflection
> *
> * @author Simon Groenewolt
> */
>
> import java.lang.reflect.*;
>
> public class InvokeTest {
>
> public void test(int i) {
> System.out.println(i);
> }
>
> public static void main(String[] arguments) {
> try {
> InvokeTest test = new InvokeTest();
>
> Class c = Class.forName("InvokeTest");
> Method m = c.getMethod("test",new Class[] {int.class});
>
> Object[] args = {new Integer(3)};
> m.invoke(test,args);
> Object[] args2 = {null};
> m.invoke(test,args2);
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> }
This class does just a simple method call using reflection, first one with a
valid Integer, then one using 'null' als the Integer.
on my computer at home:
java version "1.5.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)
> java.lang.IllegalArgumentException
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:585)
> at InvokeTest.main(InvokeTest.java:23)
Ok, that seems like what it should report.
Now the error on our webserver...
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)
> java.lang.NullPointerException
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:324)
> at InvokeTest.main(InvokeTest.java:23)
What? What! ... now it's a NPE?
Write once, have different exceptions everywhere. :-(
But at least now I know for sure that it really was a 'null' argument that was
causing the trouble, and not a 'null' object the method was invoked on.
cheers,
Simon
<<winmail.dat>>
_______________________________________________ Developers mailing list [email protected] http://lists.mmbase.org/mailman/listinfo/developers
