controlp5 examples null pointer exception and method flag

edited November 2015 in Library Questions

hello,

i was wondering about the .setGraphics() function in this example as it seems to be triggering a null pointer exception, which i haven't been able to solve.

also, in the frame(s) example, f.add(p) is also causing an issue.

does anyone know what the issue is if there is one?

p5graphics example:

import controlP5.*;

ControlP5 c1 , c2;
PGraphics panel1 , panel2;

void setup() {
  size( 1024, 500 ,P3D  );

  /* create 2 buffers */
  panel1 = createGraphics( 200 , height/2 );
  panel2 = createGraphics( 200 , height/2 );

  /* create the first instance of ControlP5 which will be rendered into panel1 */
  c1 = new ControlP5( this );
  c1.enableShortcuts();
  c1.setBackground( color( 0 , 50 ) );
  c1.addButton("hello").setSize(200,20).setPosition( 0 , 0 );
  c1.addButton("world").setSize(200,100).setPosition( 0 , 70 );
  c1.addSlider("slider").setSize(50,20).setPosition( 0 , 40 );
  c1.setGraphics( panel1 , 0 , 0 );

  /* create the second instance of ControlP5 which will be rendered into panel2 */
  c2 = new ControlP5( this );
  c2.enableShortcuts();
  c2.setBackground( color( 0 , 50 ) );
  c2.addButton("hello").setSize(200,20).setPosition( 0 , 0 );
  c2.addButton("world").setSize(200,100).setPosition( 0 , 70 );
  c2.addSlider("slider").setSize(50,20).setPosition( 0 , 40 );
  c2.setGraphics( panel2 , 220 , 0 );

}

void draw() {
  background( 100 , 0 , 0 );
  /* to change location, un-comment line below */
  //c1.setGraphics( panel1 , int(sin(frameCount*0.1) * 100) , 0 );
}

void controlEvent( ControlEvent ce) {
  println(ce);
}

frame(s) example:

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

private ControlP5 cp5;

ControlFrame cf;

int def;

void setup() {
  size(400, 400);
  cp5 = new ControlP5(this);

  // by calling function addControlFrame() a
  // new frame is created and an instance of class
  // ControlFrame is instanziated.
  cf = addControlFrame("extra", 200,200);

  // add Controllers to the 'extra' Frame inside 
  // the ControlFrame class setup() method below.
}

void draw() {
  background(def);
}

ControlFrame addControlFrame(String theName, int theWidth, int theHeight) {
  Frame f = new Frame(theName);
  ControlFrame p = new ControlFrame(this, theWidth, theHeight);
  f.add(p);
  p.init();
  f.setTitle(theName);
  f.setSize(p.w, p.h);
  f.setLocation(100, 100);
  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 {

  int w, h;

  int abc = 100;

  public void setup() {
    size(w, h);
    frameRate(25);
    cp5 = new ControlP5(this);
    cp5.addSlider("abc").setRange(0, 255).setPosition(10,10);
    cp5.addSlider("def").plugTo(parent,"def").setRange(0, 255).setPosition(10,30);
  }

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

  private ControlFrame() {
  }

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

  public ControlP5 control() {
    return cp5;
  }

  ControlP5 cp5;

  Object parent;


}

Answers

  • Answer ✓

    Hi, I assume you are using processing 3? regarding the first issue, add the type of renderer as third argument to createGraphics()

    panel1 = createGraphics( 200 , height/2, P2D );
    panel2 = createGraphics( 200 , height/2, P2D );
    

    regarding issue 2, please have a look at this discussion and also this github issue with code samples given in the comments.

  • thanks!

    i couldn't find anything when i searched and had a feeling the answer was somewhere obvious.

    cheers,

    destro

Sign In or Register to comment.