Sketch stops working when I switch to P2D

Hi,

I’m making a grid that would later be interactive. I wanted to try out some things with shaders so switched my sketch to P2D.
The second I do this I get a NullPointerException and I’m wondering why.
I’m thinking that some of the things I used are not supported by P2D but can’t figure out what.

This is my code:

PImage img, img2;
PShader edges, blur;
import processing.serial.*;

Serial serial;
public int sensorValue;
public int sensorValue2;

// KLEUR
color bg = #f1f1f1;
color red = #ff0000;
color blue = #0000ff;
color green = #00ff00;

void setup() {
  size(1440, 800,P2D);

  //serial = new Serial (this, "COM3", 9600);

  //AFBEELDINGEN
  img = loadImage("original_9c9e61510489ed24aa0624c213b18806.jpeg");
  img2 = loadImage("original_9c9e61510489ed24aa0624c213b18806.jpeg");
  imageMode(CORNER);
  
  edges = loadShader("edges.glsl");
  edges = loadShader("blur.glsl");
}

void draw() {

  //blendMode(MULTIPLY);

  frameRate(4);

  //ARDUINO VERBINDING

  //if ( serial.available() > 0 ) {
  // // Read from serial production sensor value.
  // sensorValue = serial.read();

  // println(sensorValue); //Print out the processing received sensor value

  background(bg);

  //STROKE
  //stroke(0,0,255);
  //strokeWeight(45);
  noStroke();

  //IMAGE FILTERS
  img.filter(GRAY);
  img.filter(INVERT);

  //GRID
  int tilesX = 16;
  float tilesY = 9;

  float tileW = 1600 / tilesX;
  float tileH = 900 / tilesY;

  for (int x = 0; x<tilesX; x++) {
    for ( int y = 0; y<tilesY; y++) {
      int posX = int(tileW *x);
      int posY = int(tileH *y);

      //WAVE ROOD
      float wave = cos(radians(random(frameCount*10 + x *10 +y*10)));
      float mappedWave = map(wave, -1, 1, 0, 16);
      int selector = int(mappedWave);

      //WAVE BLAUW
      float wave2 = tan(radians(random(frameCount*11 + x *10 +y*10)));
      float mappedWave2 = map(wave2, -1, 1, 0, 16);
      int selector2 = int(mappedWave2);

      //WAVE GROEN
      float wave3 = tan(radians(random(frameCount*12 + x *10 +y*10)));
      float mappedWave3 = map(wave3, -1, 1, 0, 16);
      int selector3 = int(mappedWave3);

      //WAVE AFBEELDING
      float wave4 = cos(radians(random(frameCount*13 + x *60 +y*60)));
      float mappedWave4 = map(wave4, -1, 1, 0, 3);
      int selector4 = int(mappedWave4);


      //GRID ROOD
      pushMatrix();
      frameRate(2);
      shader(edges);
      translate(posX, posY);
      if (selector == 0) {
        fill(#ff0000);
        rect(0, 0, tileW, tileH);
        //image(img, 200, 0, tileW, tileH);
      } else if (selector == 1) {
        fill(#ff0000);
        rect(0, 0, tileW, tileH);
        // image(img, width/2, height/2, tileW, tileH);
      } else if (selector == 2) {
        push();
        //noFill();
        rect(0, 0, tileW, tileH);
        pop();
      } else if (selector == 3) {
        fill(#ff0000);
        arc(0, 0, tileW*2, tileH*2, radians(0), radians(90));
      } else if (selector == 4) {
        fill(#ff0000);
        arc(tileW, 0, tileW*2, tileH*2, radians(90), radians(180));
      } else if (selector == 5) {
        fill(#ff0000);
        arc(tileW, tileH, tileW*2, tileH*2, radians(180), radians(270));
      } else if (selector == 6) {
        fill(#ff0000);
        arc(0, tileH, tileW*2, tileH*2, radians(270), radians(360));
      } else if (selector == 7) {
        fill(#ff0000);
        ellipse(tileW/2, tileH/2, tileW, tileH);
      } else if (selector == 8) {
        fill(#ff0000);
        triangle(0, tileH, 0, 0, tileW, tileH);
      } else if (selector == 9) {
        fill(#ff0000);
        triangle(0, tileH, tileW, 0, tileW, tileH);
      } else if (selector == 10) {
        fill(#ff0000);
        triangle(0, 0, tileW, 0, tileW, tileH);
      } else if (selector == 11) {
        fill(#ff0000);
        triangle(0, 0, tileW, 0, 0, tileH);
      } else if (selector == 12) {
        fill(#ff0000);
        arc(tileW/2, 0, tileW, tileH, radians(0), radians(180));
      } else if (selector == 13) {
        fill(#ff0000);
        arc(tileW/2, tileH, tileW, tileH, radians(180), radians(360));
      } else if (selector == 14) {
        fill(#ff0000);
        arc(tileW/2, tileH/2, tileW, tileH, radians(90), radians(270));
      } else if (selector == 15) {
        fill(#ff0000);
        arc(0, tileH/2, tileW, tileH, radians(270), radians(450));
      }
      // else {
      //noFill();
      //rect(0, 0, tileW, tileH);
      // }
      resetShader();
      popMatrix();

      //GRID GROEN
      pushMatrix();
      frameRate(2);

      translate(posX, posY);
      if (selector3 == 0) {
        fill(#00ff00);
        rect(0, 0, tileW, tileH);
        //image(img, 200, 0, tileW, tileH);
      } else if (selector3 == 1) {
        fill(#00ff00);
        rect(0, 0, tileW, tileH);
        // image(img, width/2, height/2, tileW, tileH);
      } else if (selector3 == 2) {
        push();
        fill(#00ff00);
        //noFill();
        rect(0, 0, tileW, tileH);
        pop();
      } else if (selector3 == 3) {
        fill(#00ff00);
        arc(0, 0, tileW*2, tileH*2, radians(0), radians(90));
      } else if (selector3 == 4) {
        fill(#00ff00);
        arc(tileW, 0, tileW*2, tileH*2, radians(90), radians(180));
      } else if (selector3 == 5) {
        fill(#00ff00);
        arc(tileW, tileH, tileW*2, tileH*2, radians(180), radians(270));
      } else if (selector3 == 6) {
        fill(#00ff00);
        arc(0, tileH, tileW*2, tileH*2, radians(270), radians(360));
      } else if (selector3 == 7) {
        fill(#00ff00);
        ellipse(tileW/2, tileH/2, tileW, tileH);
      } else if (selector3 == 8) {
        fill(#00ff00);
        triangle(0, tileH, 0, 0, tileW, tileH);
      } else if (selector3 == 9) {
        fill(#00ff00);
        triangle(0, tileH, tileW, 0, tileW, tileH);
      } else if (selector3 == 10) {
        fill(#00ff00);
        triangle(0, 0, tileW, 0, tileW, tileH);
      } else if (selector3 == 11) {
        fill(#00ff00);
        triangle(0, 0, tileW, 0, 0, tileH);
      } else if (selector3 == 12) {
        fill(#00ff00);
        arc(tileW/2, 0, tileW, tileH, radians(0), radians(180));
      } else if (selector3 == 13) {
        fill(#00ff00);
        arc(tileW/2, tileH, tileW, tileH, radians(180), radians(360));
      } else if (selector3 == 14) {
        fill(#00ff00);
        arc(tileW/2, tileH/2, tileW, tileH, radians(90), radians(270));
      } else if (selector3 == 15) {
        fill(#00ff00);
        arc(0, tileH/2, tileW, tileH, radians(270), radians(450));
      } else {
        noFill();
        rect(0, 0, tileW, tileH);
      }
      popMatrix();

      //GRID BLAUW
      pushMatrix();
      frameRate(2);

      translate(posX, posY);
      if (selector2 == 0) {
        fill(#0000ff);
        rect(0, 0, tileW, tileH);
        //image(img, 200, 0, tileW, tileH);
      } else if (selector2 == 1) {
        fill(#0000ff);
        rect(0, 0, tileW, tileH);
        // image(img, width/2, height/2, tileW, tileH);
      } else if (selector2 == 2) {

        //noFill();
        fill(#0000ff);
        rect(0, 0, tileW, tileH);
      } else if (selector2 == 3) {
        fill(#0000ff);
        arc(0, 0, tileW*2, tileH*2, radians(0), radians(90));
      } else if (selector2 == 4) {
        fill(#0000ff);
        arc(tileW, 0, tileW*2, tileH*2, radians(90), radians(180));
      } else if (selector2 == 5) {
        fill(#0000ff);
        arc(tileW, tileH, tileW*2, tileH*2, radians(180), radians(270));
      } else if (selector2 == 6) {
        fill(#0000ff);
        arc(0, tileH, tileW*2, tileH*2, radians(270), radians(360));
      } else if (selector2 == 7) {
        fill(#0000ff);
        ellipse(tileW/2, tileH/2, tileW, tileH);
      } else if (selector2 == 8) {
        fill(#0000ff);
        triangle(0, tileH, 0, 0, tileW, tileH);
      } else if (selector2 == 9) {
        fill(#0000ff);
        triangle(0, tileH, tileW, 0, tileW, tileH);
      } else if (selector2 == 10) {
        fill(#0000ff);
        triangle(0, 0, tileW, 0, tileW, tileH);
      } else if (selector2 == 11) {
        fill(#0000ff);
        triangle(0, 0, tileW, 0, 0, tileH);
      } else if (selector2 == 12) {
        fill(#0000ff);
        arc(tileW/2, 0, tileW, tileH, radians(0), radians(180));
      } else if (selector2 == 13) {
        fill(#0000ff);
        arc(tileW/2, tileH, tileW, tileH, radians(180), radians(360));
      } else if (selector2 == 14) {
        fill(#0000ff);
        arc(tileW/2, tileH/2, tileW, tileH, radians(90), radians(270));
      } else if (selector2 == 15) {
        fill(#0000ff);
        arc(0, tileH/2, tileW, tileH, radians(270), radians(450));
      } else {
        noFill();
        rect(0, 0, tileW, tileH);
      }
      popMatrix();

      //GRID AFBEELDING
      pushMatrix();
      frameRate(2);

      translate(posX, posY);
      if (selector4 == 0) {
       // fill(#000000);

        // AFBEELDING ACHTERGROND
        // image(img2, 200, 0, tileW, tileH);

        //BLEND MODES
        // blend(img, 0, 0, width, height, 0, 0, width, height, SUBTRACT);
        // blend(img, 0, 0, width, height, 0, 0, width, height, MULTIPLY);
        // blend(img, 0, 0, width, height, 0, 0, width, height, EXCLUSION);
      } else if ( selector4 ==1) {
       // img2.resize(0, int(tileW*4));
        //image(img2, 0, 0);
        //blend(img2, 0, 0, width, height, 0, 0, width, height, SUBTRACT);
      }
      popMatrix();
    }

    //VARIABELE CODE / TEKST
    textSize(50);
    //String myText = "tiles.Amount: (" + tilesX + ");";
    fill(255);

    // text(myText, 50, 70);  // Text wraps within text box
  }

  saveFrame();
}
//ANIMATIE
void mousePressed() {
  looping =  !looping;  //EDIT******
}

Thanks!

I get a null pointer exception also, but it’s because I don’t have the .jpg files. What happens when you run the following:

void setup(){
  size(1440,800,P2D);
}

void draw(){  
}

This works! Other files work as well so I think it’s something in the code that isn’t supported

The error message at the bottom of the editor should tell you who is responsible for the null pointer.

1 Like

You are overriding the edges shader in the setup

edges = loadShader("edges.glsl");
edges = loadShader("blur.glsl");

im assuming thats the issue, dont work with shaders really but i noticed you have the same variable for both of them, atleast that is one issue, if not, try keeping P2D but disabling the shaders to see if that is the issue

2 Likes

Hello @friedadedoncker,

What I did to explore this:

  • Substituted some images.
  • Borrowed same name glsl files from the Shaders examples that come with Processing.
  • Removed ALL the frameRate() calls from code in draw() and put only one frameRate(2) in setup.

I got this after doing above:

This what I see on console:

:)

1 Like

Thanks! This solved some of the issues :))

2 Likes