I cant make Sprite.addEventHandler() work. I am getting an Null pointer exception when I press the sprite button. I am aware there is an alternative method but I like the encapsulation offered by this approach.
java.lang.NullPointerException
at sprites.SMessenger.eventHandlerFailed(Unknown Source)
at sprites.SMessenger.message(Unknown Source)
at sprites.Sprite.fireEvent(Unknown Source)
at sprites.Sprite.mouseEvent(Unknown Source)
The reason the question is so vague is the exception stack trace is missing line numbers. Is it possible to rebuild the library and run with the modified version? Is there a folder in Processing that I will place the compiled class hierarchy?
I see the exception when I try to install a handler (addEventHandler) on a sprite installed on a new class. See StartButton below. Things work when the handler is installed on a sprite hosted on the PApplet.
class StartButton
{
public Sprite sprite;
StartButton(PApplet applet, int xpos, int ypos)
{
sprite = new Sprite(applet, "Start Button.png", 70);
sprite.setXY(xpos, ypos);
sprite.setVelXY(0, 0);
//sprite.setScale(.5);
sprite.respondToMouse(true);
sprite.addEventHandler(this, "onButton");
}
void onButton(Sprite sprite)
{
println("ON BUTTON xxx" + sprite);
}
}
Here is the full stack trace. No line numbers.
=======================================================
GameControlPlus V1.2.2 created by
Christian Riekoff and Peter Lager
java.lang.NullPointerException
at sprites.SMessenger.eventHandlerFailed(Unknown Source)
at sprites.SMessenger.message(Unknown Source)
at sprites.Sprite.fireEvent(Unknown Source)
at sprites.Sprite.mouseEvent(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1436)
NullPointerException
at processing.core.PApplet.handleMethods(PApplet.java:1638)
at processing.core.PApplet.handleMouseEvent(PApplet.java:2749)
at processing.core.PApplet.dequeueEvents(PApplet.java:2652)
at processing.core.PApplet.handleDraw(PApplet.java:2493)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)