NullPointer Exception when using reflection

Started by jyu, March 18, 2007, 07:20:39

Previous topic - Next topic

jyu

I'm not sure if I am writing bad code or this is a bug, but I cannot seem to get GL11.glGenLists() to work when using reflection. 

First, here is the exception when I run the app:

Exception in thread "main" java.lang.NullPointerException
        at org.lwjgl.opengl.GL11.glGenLists(GL11.java:1326)
        at Strip.<init>(Strip.java:31)
        at Strip.<init>(Strip.java:17)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
        at java.lang.Class.newInstance0(Class.java:350)
        at java.lang.Class.newInstance(Class.java:303)
        at ModelTest.<init>(ModelTest.java:72)
        at ModelTest.main(ModelTest.java:64)

Here is Strip.java:31 :

ListDescriptor =  GL11.glGenLists(1);

As you can probably deduce from the stack trace, call to glGenLists is made inside the constructor.  I instantiate the class using reflection.

Here is ModelTest.java:71-73:

cls = Class.forName(className);
object = cls.newInstance();
method = cls.getMethod("draw",null)

When I replaced the above reflection code with a regular constructor call, everthing works fine. 


ndhb

Display is created already? I assume. Maybe reflection creates a new thread? I don't know much the API though.


jyu

It's interesting that you should bring up threading.  I am not familiar with API myself, but what effect does multithreading have on GenLists?

Fool Running

QuoteIt's interesting that you should bring up threading.  I am not familiar with API myself, but what effect does multithreading have on GenLists?
OpenGL can not be accessed from multiple threads (Well, technically it can, but its not pretty ;) ). If you try to access OpenGL from a different thread than the one OpenGL was created on, you will get that exception (The null ref). So I would guess that reflection is using a different thread.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D