[ G4P ] changing Text of a Textfield in its eventHandler method

I tried the code below. When the text length is greater than 4, I’m getting an error.
For some reason, I’m not allowed to change the text, while being inside that method.
It calls the method splendidly, and the error is happening only when I’m trying to change the text.

Btw the textfield is on the secondary window.

public void textfield2_change1(GTextField source, GEvent event) {

if (event == GEvent.CHANGED) {

String text = source.getText();

if (text.length() > 4) {
  String substr = text.substring(0,4);
  source.setText(substr);
}

}
}

Here’s the error, if it be helpful.

java.lang.NullPointerException
at g4p_controls.GTextField.keyEvent(Unknown Source)
at g4p_controls.GWindow.keyEvent(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
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:1408)
at processing.core.PApplet.handleMethods(PApplet.java:1610)
at processing.core.PApplet.handleKeyEvent(PApplet.java:2946)
at processing.core.PApplet.dequeueEvents(PApplet.java:2599)
at processing.core.PApplet.handleDraw(PApplet.java:2437)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

Trying to programmatically change the displayed text in GTextField or GTextArea while the inside the change-event handler has unpredictable results so should not be done. It is not a bug just a limitation of G4P

1 Like