Controlp5 - error java.lang.NullPointerException

Hi guys! Im Niko, new here :slight_smile:

I am trying to make an application where I mix together images of different lamps and lighting conditions. The way I imagined it, is that each there is one channel with the natural light, and slider01 controls its intensity, and then on top of that we get different lighting options that are I can turn on by pressing either of the three buttons. A button then brings up a slider where I can control intensity of that image. Finally, pressing other buttons brings up another slider in place of previous, and resets value from the previous, so we start with the clear ‘‘canvas’’ of only the image from the first channel (the one that is independent of the buttons).

It is kinda working, but I am getting a bunch of errors coming from the buttons - do you have any idea what is the issue here? Also, the app is kinda heavy on the computer… I am not sure but maybe there is a way to make it run more smooth? Any help would be greatly appreciated!

The errors:

sty 02, 2020 12:41:41 PM controlP5.ControlBroadcaster printMethodError
SEVERE: An error occured while forwarding a Controller event, please check your code at CCT4000K
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.callTarget(Unknown Source)
	at controlP5.ControlBroadcaster.broadcast(Unknown Source)
	at controlP5.Controller.broadcast(Unknown Source)
	at controlP5.Button.setValue(Unknown Source)
	at LIGHTCONTROL.setup(LIGHTCONTROL.java:72)
	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 LIGHTCONTROL.CCT4000K(LIGHTCONTROL.java:223)
	... 13 more
sty 02, 2020 12:41:41 PM controlP5.ControlBroadcaster printMethodError
SEVERE: An error occured while forwarding a Controller event, please check your code at CCT5000K
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.callTarget(Unknown Source)
	at controlP5.ControlBroadcaster.broadcast(Unknown Source)
	at controlP5.Controller.broadcast(Unknown Source)
	at controlP5.Button.setValue(Unknown Source)
	at LIGHTCONTROL.setup(LIGHTCONTROL.java:83)
	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 LIGHTCONTROL.CCT5000K(LIGHTCONTROL.java:234)
	... 13 more

Code:

//In case of any questions - contact the author via: derengowski.nikodem@gmail.com
import controlP5.*;
ControlP5 cp5;
ControlP5 OPT1;
ControlP5 OPT2;
ControlP5 OPT3;

PImage img1;
PImage img2;
PImage img3;
PImage img4;
PImage img5;

int Daylight = 0;
int Tasklamp = 0;
int Ceiling3000K = 0;
int Ceiling4000K = 0;
int Ceiling5000K = 0;

boolean display3K = false;
boolean display4K = false;
boolean display5K = false;

void setup() {
  //size(960, 680);
  fullScreen();
 OPT1 = new ControlP5(this);
 OPT2 = new ControlP5(this);
 OPT3 = new ControlP5(this);
 cp5 = new ControlP5(this);
 
 PFont p = createFont("Calibri bold",10); 
 ControlFont font = new ControlFont(p);
 cp5.setFont(font);
 OPT1.setFont(font);
 OPT2.setFont(font);
 OPT3.setFont(font);
 
  cp5.addButton("CCT3000K")
   .setPosition(20,height-90)
   .setSize(80,50)
   .setValue(0)
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(255, 214, 170, 230))
   .activateBy(ControlP5.PRESS);
   
   ;
   
  cp5.addButton("CCT4000K")
   .setPosition(120,height-90)
   .setSize(80,50)
   .setValue(0)
       .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(244, 255, 250, 230))
   .activateBy(ControlP5.PRESS);
   ;
   
   cp5.addButton("CCT5000K")
   .setPosition(220,height-90)
   .setSize(80,50)
   .setValue(0)
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(212, 235, 255, 230))
   .activateBy(ControlP5.PRESS);
   ;
   
  cp5.addSlider("Daylight")
  .setRange(0, 255)
  .setValue(0)
    .setPosition(20, height - 140) 
    .setSize(width - 200, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  cp5.addSlider("Tasklamp")
   .setRange(0, 210)
    .setPosition(320, height-60) 
    .setSize(width - 500, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  OPT1.addSlider("Ceiling3000K")
  .setRange(0, 230)
    .setPosition(320, height - 100) 
    .setSize(width-500, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  OPT2.addSlider("Ceiling4000K")
   .setRange(0, 230)
    .setPosition(320, height - 100) 
    .setSize(width - 500, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  OPT3.addSlider("Ceiling5000K")
     .setRange(0, 230)
    .setPosition(320, height - 100) 
    .setSize(width - 500, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  img1 = loadImage("2GDLx.png");
  img1.resize(0, height-150);
  img2 = loadImage("2GTx.png");
  img2.resize(0, height-150);
  img3 = loadImage("2G3Kx.png");
  img3.resize(0, height-150);
  img4 = loadImage("2G4K.png");
  img4.resize(0, height-150);
  img5 = loadImage("2G5K.png");
  img5.resize(0, height-150);
}

void draw() {
  
  
  background(0);
  fill(255);
  textSize(14);
  text("CCT OF CEILING LUMINARIES", 20, height - 100); 
  blendMode(SCREEN);
int imgw = 20;
int imgh = 0;
  tint(255, 255-Daylight);
  image(img1,imgw,imgh);

  tint(255, Tasklamp);
  image(img2, imgw,imgh);
  
  tint(255, Ceiling3000K);
  image(img3, imgw,imgh);
  
  tint(255, Ceiling4000K);
  image(img4, imgw,imgh);
  
  tint(255, Ceiling5000K);
  image(img5, imgw,imgh);
  
hideSlider3K();
hideSlider4K();
hideSlider5K();
}

void hideSlider3K() {

  if (display3K == false) {
    OPT1.hide();
  }
  else {
    OPT1.show();
  }
}

void hideSlider4K() {

  if (display4K == false) {
    OPT2.hide();
  }
  else {
    OPT2.show();
  }
}
void hideSlider5K() {

  if (display5K == false) {
    OPT3.hide();
  }
  else {
    OPT3.show();
  }
}

public void CCT3000K(){
display3K = true;
display4K = false;
display5K = false;
OPT2.getController("Ceiling4000K").getValue();;
OPT2.getController("Ceiling4000K").setValue(0);
OPT3.getController("Ceiling5000K").getValue();;
OPT3.getController("Ceiling5000K").setValue(0);
println("3K");
}

public void CCT4000K(){
display3K = false;
display4K = true;
display5K = false;
OPT1.getController("Ceiling3000K").getValue();;
OPT1.getController("Ceiling3000K").setValue(0);
OPT3.getController("Ceiling5000K").getValue();;
OPT3.getController("Ceiling5000K").setValue(0);
println("4K");
}

public void CCT5000K(){
display3K = false;
display4K = false;
display5K = true;
OPT1.getController("Ceiling3000K").getValue();;
OPT1.getController("Ceiling3000K").setValue(0);
OPT2.getController("Ceiling4000K").getValue();;
OPT2.getController("Ceiling4000K").setValue(0);
println("5K");
}

sorry, the OPTx you try would need some explanation.

please put this idea,
and the whole sketch aside for a while

and try a new one:

cp5 one button
cp5 one slider

also please use the better style

import controlP5.*;
ControlP5 cp5;
Slider s1;
Button b1;

void setup() {
  //size(960, 680);
 //  fullScreen();
// NEVER GIVE US BAD CODE IN FULLSCREEN
 cp5 = new ControlP5(this);
 s1 = cp5.addSlider("mys1");
 b1 = cp5.addButton("myb1")    
    .setPosition(10, height-50)
    .setSize(40, 40)
    .setValue(50)
;
}

void draw(){}
void myb1() {
  println("myb1 click");
  println( s1.getValue() );
}

so you later NOT need to use

//cp5.getController("mys1").getValue();
println( s1.getValue() );

Dear kll, thanks for reply!

I read my post again and indeed, it is confusing. I’ll try in bulletpoints:

  • There are 5 images in total, and they are blending together with screen mode
  • There is one image as a background DAYLIGHT, and a slider that controls its visibility (tint (255, 255-Slider1value))
  • There is one image as a TASKLAMP, and slider with its name that controls its visibility/blending (tint (255, slider2value)))
  • There should be 3 buttons, each causing a slider 3, 4, or 5 to appear that control visibility of images 3,4, and 5. (button 1 pops up slider 1, which controls the blending of image 3. Now, if you press button 2, then it does following:
    a) hides and resets value of slider 3, b) shows slider 4 that controls blending of image 4.
  • Analogically, button 3 does the same with image 5 and slider 5)

When i remove the other 2 buttons, the code seems to work without problem:

import controlP5.*;
ControlP5 cp5;
ControlP5 OPT1;

PImage img1;
PImage img2;
PImage img3;


int Daylight = 0;
int Tasklamp = 0;
int Ceiling3000K = 0;


boolean display3K = false;


void setup() {
  size(960, 680);

 OPT1 = new ControlP5(this);
 cp5 = new ControlP5(this);
 
 PFont p = createFont("Calibri bold",10); 
 ControlFont font = new ControlFont(p);
 cp5.setFont(font);
 OPT1.setFont(font);

 
  cp5.addButton("CCT3000K")
   .setPosition(20,height-90)
   .setSize(80,50)
   .setValue(0)
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(255, 214, 170, 230))
   .activateBy(ControlP5.PRESS);
   
   ;
   
  
  cp5.addSlider("Daylight")
  .setRange(0, 255)
  .setValue(0)
    .setPosition(20, height - 140) 
    .setSize(width - 200, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  cp5.addSlider("Tasklamp")
   .setRange(0, 210)
    .setPosition(320, height-60) 
    .setSize(width - 500, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  OPT1.addSlider("Ceiling3000K")
  .setRange(0, 230)
    .setPosition(320, height - 100) 
    .setSize(width-500, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));

   
  img1 = loadImage("DL1.JPG");
  img1.resize(0, height-150);
  img2 = loadImage("TL1.JPG");
  img2.resize(0, height-150);
  img3 = loadImage("NL1.JPG");
  img3.resize(0, height-150);
}

void draw() {
  
  
  background(0);
  fill(255);
  textSize(14);
  text("CCT OF CEILING LUMINARIES", 20, height - 100); 
  blendMode(SCREEN);
int imgw = 20;
int imgh = 0;
  tint(255, 255-Daylight);
  image(img1,imgw,imgh);

  tint(255, Tasklamp);
  image(img2, imgw,imgh);
  
  tint(255, Ceiling3000K);
  image(img3, imgw,imgh);
  
  
hideSlider3K();

}

void hideSlider3K() {

  if (display3K == false) {
    OPT1.hide();
  }
  else {
    OPT1.show();
  }
}


public void CCT3000K(){
display3K = true;


}


The problem starts when I want to add other button, that would reset value from slider 3 (button 1), and pop up slider 4 controlling blending of image 4.

Here is the big code again, with removed PrintIn and get value (you were right, no need to use them!)

import controlP5.*;
ControlP5 cp5;
ControlP5 OPT1;
ControlP5 OPT2;
ControlP5 OPT3;

PImage img1;
PImage img2;
PImage img3;
PImage img4;
PImage img5;

int Daylight = 0;
int Tasklamp = 0;
int Ceiling3000K = 0;
int Ceiling4000K = 0;
int Ceiling5000K = 0;

boolean display3K = false;
boolean display4K = false;
boolean display5K = false;

void setup() {
  size(960, 680);
  //fullScreen();
 OPT1 = new ControlP5(this);
 OPT2 = new ControlP5(this);
 OPT3 = new ControlP5(this);
 cp5 = new ControlP5(this);
 
 PFont p = createFont("Calibri bold",10); 
 ControlFont font = new ControlFont(p);
 cp5.setFont(font);
 OPT1.setFont(font);
 OPT2.setFont(font);
 OPT3.setFont(font);
 
  cp5.addButton("CCT3000K")
   .setPosition(20,height-90)
   .setSize(80,50)
   .setValue(0)
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(255, 214, 170, 230))
   .activateBy(ControlP5.PRESS);
   
   ;
   
  cp5.addButton("CCT4000K")
   .setPosition(120,height-90)
   .setSize(80,50)
   .setValue(0)
       .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(244, 255, 250, 230))
   .activateBy(ControlP5.PRESS);
   ;
   
   cp5.addButton("CCT5000K")
   .setPosition(220,height-90)
   .setSize(80,50)
   .setValue(0)
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(212, 235, 255, 230))
   .activateBy(ControlP5.PRESS);
   ;
   
  cp5.addSlider("Daylight")
  .setRange(0, 255)
  .setValue(0)
    .setPosition(20, height - 140) 
    .setSize(width - 200, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  cp5.addSlider("Tasklamp")
   .setRange(0, 210)
    .setPosition(320, height-60) 
    .setSize(width - 500, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  OPT1.addSlider("Ceiling3000K")
  .setRange(0, 230)
    .setPosition(320, height - 100) 
    .setSize(width-500, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  OPT2.addSlider("Ceiling4000K")
   .setRange(0, 230)
    .setPosition(320, height - 100) 
    .setSize(width - 500, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  OPT3.addSlider("Ceiling5000K")
     .setRange(0, 230)
    .setPosition(320, height - 100) 
    .setSize(width - 500, 20)    
    .setColorValue(color(200))
    .setColorActive(color(155))
    .setColorForeground(color(100))
    .setColorBackground(color(150, 150, 150));
    
  img1 = loadImage("DL1.JPG");
  img1.resize(0, height-150);
  img2 = loadImage("TL1.JPG");
  img2.resize(0, height-150);
  img3 = loadImage("NL1.JPG");
  img3.resize(0, height-150);
  img4 = loadImage("2G4K.png");
  img4.resize(0, height-150);
  img5 = loadImage("2G5K.png");
  img5.resize(0, height-150);
}

void draw() {
  
  
  background(0);
  fill(255);
  textSize(14);
  text("CCT OF CEILING LUMINARIES", 20, height - 100); 
  blendMode(SCREEN);
int imgw = 20;
int imgh = 0;
  tint(255, 255-Daylight);
  image(img1,imgw,imgh);

  tint(255, Tasklamp);
  image(img2, imgw,imgh);
  
  tint(255, Ceiling3000K);
  image(img3, imgw,imgh);
  
  tint(255, Ceiling4000K);
  image(img4, imgw,imgh);
  
  tint(255, Ceiling5000K);
  image(img5, imgw,imgh);
  
hideSlider3K();
hideSlider4K();
hideSlider5K();
}

void hideSlider3K() {

  if (display3K == false) {
    OPT1.hide();
  }
  else {
    OPT1.show();
  }
}

void hideSlider4K() {

  if (display4K == false) {
    OPT2.hide();
  }
  else {
    OPT2.show();
  }
}
void hideSlider5K() {

  if (display5K == false) {
    OPT3.hide();
  }
  else {
    OPT3.show();
  }
}

public void CCT3000K(){
display3K = true;
display4K = false;
display5K = false;
OPT2.getController("Ceiling4000K").setValue(0);
OPT3.getController("Ceiling5000K").setValue(0);
}

public void CCT4000K(){
display3K = false;
display4K = true;
display5K = false;
OPT1.getController("Ceiling3000K").setValue(0);
OPT3.getController("Ceiling5000K").setValue(0);
}

public void CCT5000K(){
display3K = false;
display4K = false;
display5K = true;
OPT1.getController("Ceiling3000K").setValue(0);
OPT2.getController("Ceiling4000K").setValue(0);
}

I hope it makes more sense now!