[SOLVED]Nifty NullPointerException in canHandleInteraction

I have a panel where different buttons are displayed and one gets clicked then they are all removed.

Then many other buttons are displayed, clicked, removed. And so on.

I sometimes (like 20% of the times) get the following exception when user click on a button.

[java]

6-set-2011 16.30.45 com.jme3.app.Application handleError

GRAVE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at de.lessvoid.nifty.elements.Element.canHandleInteraction(Element.java:1445)

at de.lessvoid.nifty.elements.Element.mouseEvent(Element.java:1376)

at de.lessvoid.nifty.screen.MouseOverHandler.processMouseEvent(MouseOverHandler.java:101)

at de.lessvoid.nifty.screen.Screen.forwardMouseEventToLayers(Screen.java:360)

at de.lessvoid.nifty.screen.Screen.mouseEvent(Screen.java:336)

at de.lessvoid.nifty.Nifty.forwardMouseEventToScreen(Nifty.java:245)

at de.lessvoid.nifty.Nifty.access$1300(Nifty.java:72)

at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processEvent(Nifty.java:1344)

at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processMouseEvent(Nifty.java:1311)

at com.jme3.niftygui.InputSystemJme.onMouseMotionEventQueued(InputSystemJme.java:106)

at com.jme3.niftygui.InputSystemJme.forwardEvents(InputSystemJme.java:169)

at de.lessvoid.nifty.Nifty.update(Nifty.java:227)

at com.jme3.niftygui.InputSystemJme.endInput(InputSystemJme.java:94)

at com.jme3.input.InputManager.processQueue(InputManager.java:773)

at com.jme3.input.InputManager.update(InputManager.java:837)

at com.jme3.app.Application.update(Application.java:567)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:235)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:144)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:218)

at java.lang.Thread.run(Thread.java:679)

[/java]

I suppose its related to the time of update of the form: being that, out of the update() method of jME, the exception is thrown.

Is my hypothesis correct? How can I resolve this issue?

Thanks

I’m not sure if this is the same kind of issue I’m having or if it’s related, but it seems Nifty sometimes get stuck while loading a screen. Can’t explain it in any other way.



Fortunately it doesn’t occur very often, 1 in 15/20 if that.



That part (getScreenElement) sometimes is null, no idea why. Could be the same issue we’re both having.

[java]

left = gMgrs.getGuiManager().getScreenElement(“previewDraggable#panel-content”).getX() + 4;

top = gMgrs.getGuiManager().getScreenElement(“previewDraggable#panel-content”).getY();

[/java]

I don’t know if your issue is related. Are you getting the same exact exception?

Anyway, looking in the source code the null thing is the screen of the element.

[java]

private boolean canHandleInteraction() {



return enabled && !screen.isEffectActive(EffectEventId.onStartScreen) && !screen.isEffectActive(EffectEventId.onEndScreen);



}

[/java]



So the element is unbound but the nifty event processor still tries to forward events to it. Is this supposition right? Any workaround?

I know it’s not quite the same thing you’re having, but I’m wondering if the root cause isn’t the same since the symptoms feel the same to me and since it’s intermittent.

I’m only getting a NPE when I reach the first line in the snippet of my previous post. Since I posted the above, it happened only once, like 1 minute ago.

@madjack,



Ik think your problem lies in the moment you call those methods. Since there are several points in the Nifty screencontroller where you can add code of your own. (init, startsreen, bind, …) Each of these methods is called by Nifty at a different time in the creation / loading of an element. In your case, the probable reason is that you have the code in the wrong method. Which sometimes results in the code being called before the element is bound.

I’ve had this with one of my own controllers. Moving the code to the bind method solved it for me.

Related (unresolved) topic:



http://hub.jmonkeyengine.org/groups/gui/forum/topic/nullpointerexception-with-nifty/

I resolved the issue making sure that the panel update code runs when SimpleApplication.update() is called. Previously I was running it in a thread and the code worked or not depending at which time the thread received priority.

So, sticky note: dynamic Nifty GUIs must be updated in SimpleApplication.update().

Is this written somewhere I didn’t read?

ah yes, Nifty is NOT thread safe…



If you had told me that you were running it in a seperate thread…

ractoc said:
@madjack,

Ik think your problem lies in the moment you call those methods. Since there are several points in the Nifty screencontroller where you can add code of your own. (init, startsreen, bind, ...) Each of these methods is called by Nifty at a different time in the creation / loading of an element. In your case, the probable reason is that you have the code in the wrong method. Which sometimes results in the code being called before the element is bound.
I've had this with one of my own controllers. Moving the code to the bind method solved it for me.

Geez, just seen your reply. O_o

That's unfortunately not the problem as these two lines (previous post) are not in nifty's screen controller. They're in a class that is initialized during the first frame while doing the AppStates of the game. When that method is triggered the renderer loop is running and the GUI should be loaded at that point, but something either breaks as the screen is loaded or somehow the screen becomes corrupt or something else. As I said, it's extremely rare and because of that, is very low priority.

For testing purpose I inserted:
[java]while (element == null) {
System.out.println("waiting for element to be non-null");
}[/java]
essentially a wait, but it kept waiting, the screen never came up and got spammed with the message until I stopped the game...

One possible fix would be to wait until I raised a flag meaning that the screen has been fully loaded, but from the result of the test above I doubt that would work. I'm not sure yet how I'll handle this since I have no idea what's triggering it. We'll see when/if I find the trouble I guess