g4p NullPointerException on event handler

I'm trying to set a default max and min range of a value that can be in a textfield if user enters val > 350, then setText to 350,..etc

getting:

java.lang.NullPointerException
at g4p_controls.GTextField.keyEvent(Unknown Source)
at g4p_controls.GWindowImpl.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:1427)
at processing.core.PApplet.handleMethods(PApplet.java:1622)
at processing.core.PApplet.handleKeyEvent(PApplet.java:2957)
at processing.core.PApplet.dequeueEvents(PApplet.java:2610)
at processing.core.PApplet.handleDraw(PApplet.java:2448)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

code:

int TEMP_MAX = 350;
int TEMP_MIN = 100;

public void textfield1_change1(GTextField source, GEvent event) { 
    println("textfield1 - GTextField >> GEvent." + event + " @ " + millis());
    if (event == GEvent.CHANGED) {
        int tempValue = int(textfield1.getText());      //convert string to int
        if (tempValue < 0)
            source.setText(str(TEMP_MIN));
        else if (tempValue > TEMP_MAX)
            source.setText(str(TEMP_MAX));
    }
}


public void createGUI(){
  G4P.messagesEnabled(false);
  G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
  G4P.setCursor(ARROW);
  surface.setTitle("Sketch Window");
  textfield1 = new GTextField(this, 121, 63, 160, 30, G4P.SCROLLBARS_NONE);
  textfield1.setOpaque(true);
  textfield1.addEventHandler(this, "textfield1_change1");
}

GTextField textfield1;

Answers

  • Answer ✓

    When the textfield has focus (i.e. it has the text-insertion cursor) you are not allowed to change the value stored in the textfield programmatically using setText.

  • Hi quark, I have a similar problem where i need to set a text field which has focus. Curiously it has always worked on Windows but on a macOS it gives the Null pointer exception.

    Is there a work around?

    Phil.

  • I am afraid there is no work around and I have no idea why it should work with Windows. :(

  • Thanks. As you say very strange why Windows doesn't give this error :-O

Sign In or Register to comment.