Guibuilder -> Event -> NullPointerException

Hi!

this code produces a NullPointerException:

public void tf_step_delay_change(GTextField source, GEvent event) { //_CODE_:tf_step_delay:234478:
  if(event == GEvent.ENTERED){
     slider_step_delay.setValue(Integer.valueOf(source.getText()));
  }
} //_CODE_:tf_step_delay:234478:
java.lang.NullPointerException
	at g4p_controls.GTextField.keyEvent(Unknown Source)
	at g4p_controls.GWindowImpl.sendKeyEvent(Unknown Source)
	at g4p_controls.GWindowImpl.keyEvent(Unknown Source)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1395)
	at processing.core.PApplet.handleMethods(PApplet.java:1549)
	at processing.core.PApplet.handleKeyEvent(PApplet.java:2797)
	at processing.core.PApplet.dequeueEvents(PApplet.java:2385)
	at processing.core.PApplet.handleDraw(PApplet.java:2220)
	at processing.awt.PSurfaceAWT$10.callDraw(PSurfaceAWT.java:1419)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:354)
NullPointerException

Why?

I can’t see how that exception can be created from that event handler.

If the slider slider_step_delay has been created before this method has been executed then there is no code inside the event handler that could generate a NPE.

I assume that you are not calling the keyEvent method directly from your own code. That is not permitted.

If an exception had been thrown in this event handler you would get a stack trace like

################  EXCEPTION IN EVENT HANDLER  ################
An error occured during execution of the eventhandler:
CLASS: sketch_210905a   METHOD: tf_step_delay_change
	Caused by java.lang.NumberFormatException: null
	java.lang.Integer.parseInt(Integer.java:542)
	java.lang.Integer.valueOf(Integer.java:766)
	sketch_210905a.tf_step_delay_change(sketch_210905a.java:63)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:498)
	g4p_controls.GAbstractControl.fireEvent(Unknown Source)
	g4p_controls.GTextField.keyTypedProcess(Unknown Source)
	g4p_controls.GTextField.keyEvent(Unknown Source)
	g4p_controls.GWindowImpl.sendKeyEvent(Unknown Source)
	g4p_controls.GWindowImpl.keyEvent(Unknown Source)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:498)
	processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1436)
	processing.core.PApplet.handleMethods(PApplet.java:1638)
	processing.core.PApplet.handleKeyEvent(PApplet.java:3021)
	processing.core.PApplet.dequeueEvents(PApplet.java:2655)
	processing.core.PApplet.handleDraw(PApplet.java:2493)
	processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
	processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
##############################################################
3 Likes