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

**URL:** <https://discourse.processing.org/t/g4p-changing-text-of-a-textfield-in-its-eventhandler-method/1170>\
**Category:** Libraries\
**Created:** [June 22, 2018, 1:59pm UTC](https://discourse.processing.org/t/g4p-changing-text-of-a-textfield-in-its-eventhandler-method/1170 "2018-06-22T13:59:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AntonC](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@AntonC](https://discourse.processing.org/u/AntonC)\
**Post date:** [June 22, 2018, 1:59pm UTC](https://discourse.processing.org/t/g4p-changing-text-of-a-textfield-in-its-eventhandler-method/1170/1 "2018-06-22T13:59:49Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [June 23, 2018, 11:51am UTC](https://discourse.processing.org/t/g4p-changing-text-of-a-textfield-in-its-eventhandler-method/1170/2 "2018-06-23T11:51:16Z")

</div>

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
