Why does this code occasionally throw a null pointer exception?

edited August 2015 in Library Questions

This code works most time. But every now and then ( maybe 1 out of 6 or 1 out of 10) I will get a null pointer exception on this line (line 14) :

 cf.control().addButton("Load",1,5,10,60,20).plugTo(this);

Is there anyway to fix this?

import java.awt.Frame;
import java.awt.BorderLayout;
import controlP5.*;

private ControlP5 cp5;

ControlFrame cf;
ColorPicker cp;
void setup() {

     size(200, 200);
     cp5 = new ControlP5(this);
     cf = addControlFrame("Tools");
     cf.control().addButton("Load",1,5,10,60,20).plugTo(this);
     cf.control().addButton("Save",1,5,35,60,20).plugTo(this);
     cp = cf.control().addColorPicker("picker").setPosition(10, 300)
     .setColorValue(color(255, 128, 0, 128)).plugTo(this);    

}
void draw(){
     background(100);
}



void controlEvent(ControlEvent theEvent) {
     if(theEvent.isController()) { 
          print("control event from : "+theEvent.controller().getName());
          println(", value : "+theEvent.controller().getValue());
     }
}     


ControlFrame addControlFrame(String theName) {
  Frame f = new Frame(theName);
  ControlFrame p = new ControlFrame(this, 300, 400);
  f.add(p);
  p.init();
  f.setTitle(theName);
  f.setSize(p.w, p.h);
  f.setLocation(120, 120);
  f.setResizable(false);
  f.setVisible(true);
  return p;
}


// the ControlFrame class extends PApplet, so we 
// are creating a new processing applet inside a
// new frame with a controlP5 object loaded
public class ControlFrame extends PApplet {

  ControlP5 cp5;

  Object parent;

  int w, h;

  int abc;

  private ControlFrame() {
  }

  ControlFrame(Object theParent, int theWidth, int theHeight) {
    parent = theParent;
    w = theWidth;
    h = theHeight;
  }


  public ControlP5 control() {
    return cp5;
  }
  public void setup() {
    size(w, h);
    frameRate(25);
    cp5 = new ControlP5(this);
  }

  public void draw() {
      background(abc);
  }
}
Tagged:

Answers

  • I fixed the exception but its created a new problem. I moved the .addbutton calls from the main setup() to the ControlFrame class setup() and plugged them into the parent of the control frame. The occasional null pointer exception is now gone. But now I have a new problem. In my program, there are image buttons and Process can't find the data folder from the setup() in the ControlFrame class. How do I fix this?

    import java.awt.Frame;
    import java.awt.BorderLayout;
    import controlP5.*;
    
    private ControlP5 cp5;
    
    ControlFrame cf;
    ColorPicker cp;
    void setup() {
    
         size(200, 200);
         cp5 = new ControlP5(this);
         cf = addControlFrame("Tools");
    
    }
    void draw(){
         background(100);
    }
    
    
    
    void controlEvent(ControlEvent theEvent) {
         if(theEvent.isController()) { 
              print("control event from : "+theEvent.controller().getName());
              println(", value : "+theEvent.controller().getValue());
         }
    }     
    
    
    ControlFrame addControlFrame(String theName) {
      Frame f = new Frame(theName);
      ControlFrame p = new ControlFrame(this, 300, 400);
      f.add(p);
      p.init();
      f.setTitle(theName);
      f.setSize(p.w, p.h);
      f.setLocation(120, 120);
      f.setResizable(false);
      f.setVisible(true);
      return p;
    }
    
    
    // the ControlFrame class extends PApplet, so we 
    // are creating a new processing applet inside a
    // new frame with a controlP5 object loaded
    public class ControlFrame extends PApplet {
    
      ControlP5 cp5;
    
      Object parent;
    
      int w, h;
    
      int abc;
    
      private ControlFrame() {
      }
    
      ControlFrame(Object theParent, int theWidth, int theHeight) {
        parent = theParent;
        w = theWidth;
        h = theHeight;
      }
    
    
      public ControlP5 control() {
        return cp5;
      }
      public void setup() {
        size(w, h);
        frameRate(25);
        cp5 = new ControlP5(this);
        cp5.addButton("Load",1,5,10,60,20).plugTo(parent);
        cp5.addButton("Save",1,5,35,60,20).plugTo(parent);
    
        //Process cant find the images when the buttons are added here. It can find the images
       //when the buttons are added in the main setup(), but that causes spraratic null
       // pointer exceptions. Is this quirky or am I just not getting it?
    
      //  cp5.addButton("buttonA")
      //   .setPosition(175,275)
     //    .setImages(loadImage("Arrow-Left.png"), loadImage("Arrow-Right.png"), loadImage("Refresh.png"))
    //     .updateSize();
        cp = cp5.addColorPicker("picker").setPosition(10, 300)
         .setColorValue(color(255, 128, 0, 128)).plugTo(parent);    
      }
    
      public void draw() {
          background(abc);
      }
    }
    
  • I think I'm going to try G4P

  • Which version of Processing are you using?

  • I'm using 2.2.1.

    Are you the quark from quark's place? If so I'm watching your tutorial on the G4P GUI builder right now :)

  • yes he is

  • Answer ✓

    Yes I am :D

    Hope you enjoy the video. If you plan to use GUI Builder take your time, play with it and experiment with the controls you want to use. It is better to do this before you attempt to use it in a serious project.

  • Its very intuitive. I'm very pleased with it.You did a great job with it.

Sign In or Register to comment.