# Sketch stops working when I switch to P2D

**URL:** <https://discourse.processing.org/t/sketch-stops-working-when-i-switch-to-p2d/37120>\
**Category:** Coding Questions\
**Created:** [May 26, 2022, 2:47pm UTC](https://discourse.processing.org/t/sketch-stops-working-when-i-switch-to-p2d/37120 "2022-05-26T14:47:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![friedadedoncker](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@friedadedoncker](https://discourse.processing.org/u/friedadedoncker)\
**Post date:** [May 26, 2022, 2:47pm UTC](https://discourse.processing.org/t/sketch-stops-working-when-i-switch-to-p2d/37120/1 "2022-05-26T14:47:00Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [May 26, 2022, 3:31pm UTC](https://discourse.processing.org/t/sketch-stops-working-when-i-switch-to-p2d/37120/2 "2022-05-26T15:31:20Z")

</div>

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:

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

void draw(){  
}

```

---

<div class="post-metadata">

**Author:** ![friedadedoncker](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@friedadedoncker](https://discourse.processing.org/u/friedadedoncker)\
**Post date:** [May 26, 2022, 3:33pm UTC](https://discourse.processing.org/t/sketch-stops-working-when-i-switch-to-p2d/37120/3 "2022-05-26T15:33:24Z")

</div>

> [@svan](#):
>
> ```auto
> 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

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [May 26, 2022, 3:38pm UTC](https://discourse.processing.org/t/sketch-stops-working-when-i-switch-to-p2d/37120/4 "2022-05-26T15:38:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)\
**Post date:** [May 26, 2022, 4:40pm UTC](https://discourse.processing.org/t/sketch-stops-working-when-i-switch-to-p2d/37120/5 "2022-05-26T16:40:48Z")

</div>

You are overriding the edges shader in the setup

```auto
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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 26, 2022, 5:07pm UTC](https://discourse.processing.org/t/sketch-stops-working-when-i-switch-to-p2d/37120/6 "2022-05-26T17:07:16Z")

</div>

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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/8/c8e770c4b2cf9b1bcace6e5b476ad648e52fac3b.png)

This what I see on console:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/c/ecae1fc541ce1c8ca252f1aeb996efba93c3cb50.png)

`:)`

---

<div class="post-metadata">

**Author:** ![friedadedoncker](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@friedadedoncker](https://discourse.processing.org/u/friedadedoncker)\
**Post date:** [May 26, 2022, 7:40pm UTC](https://discourse.processing.org/t/sketch-stops-working-when-i-switch-to-p2d/37120/7 "2022-05-26T19:40:01Z")

</div>

Thanks! This solved some of the issues :))

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 26, 2022, 9:06pm UTC](https://discourse.processing.org/t/sketch-stops-working-when-i-switch-to-p2d/37120/8 "2022-05-26T21:06:30Z")

</div>

> **[r/processing - Sketch stops working when I switch to P2D](https://www.reddit.com/r/processing/comments/uy9g0j/sketch_stops_working_when_i_switch_to_p2d/)**
>
> 1 vote and 2 comments so far on Reddit
