ControlP5 - java.lang.NullPointerException

Hello,

Similar to this post, i have an error.
I know the problem is path = controlP5.get(Textfield.class,"File").getText();

But i don’t understand why. All variables are declared at right time… i think :stuck_out_tongue:

It’s not blocking but annoying in the terminal

Thanks for help.

sept. 13, 2019 8:33:12 PM controlP5.ControlBroadcaster printMethodError
GRAVE: An error occured while forwarding a Controller event, please check your code at controlEvent
java.lang.reflect.InvocationTargetException
	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 controlP5.ControlBroadcaster.invokeMethod(Unknown Source)
	at controlP5.ControlBroadcaster.broadcast(Unknown Source)
	at controlP5.Controller.broadcast(Unknown Source)
	at controlP5.Button.setValue(Unknown Source)
	at ocm_app.createGui(ocm_app.java:45)
	at ocm_app.setup(ocm_app.java:24)
	at processing.core.PApplet.handleDraw(PApplet.java:2425)
	at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
	at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
Caused by: java.lang.NullPointerException
	at ocm_app.controlEvent(ocm_app.java:63)
	... 13 more

here is my code:
main:

boolean smartphone = true;
String path;
void setup(){
  size(600,400);
  
  createGui();
}

void draw(){
}

gui:

import controlP5.*; // import controlP5 library
ControlP5 controlP5; // controlP5 object


void createGui() {
  controlP5 = new ControlP5(this);
  int x_control = int(width * 0.01);
  int y_control = int(height *0.95);
  int w_button = int(width * 0.1);
  int h_button = int(width * 0.03);

 controlP5.addButton("bt_connect")
    .setValue(0)
    .setPosition(x_control, y_control)
    .setSize( w_button, h_button);
  controlP5.addButton("open")
    .setValue(0)
    .setPosition(x_control*2 + w_button, y_control)
    .setSize( w_button, h_button);
  controlP5.addTextfield("File")
    .setValue("test.txt")
    .setPosition(x_control*3 + w_button*2, y_control)
    .setSize(w_button, h_button)
    .setInputFilter(ControlP5.STRING);
}


void controlEvent(ControlEvent theEvent) {
  if (theEvent.isController()) { 

    print("control event from : "+theEvent.getController().getName());
    println(", value : "+theEvent.getController().getValue());
    
    if (theEvent.getController().getName()=="open") {     
      path = controlP5.get(Textfield.class,"File").getText();
    }
  }
}
1 Like

i think the point it that CP5
fires events at startup
BUT at
addButton OPEN
addTextfield FILE
not declared/happened.


move it UP