0

so I'm creating a code. In it, I wanted to make two eyes for the helmet. To do it, I need to create a function so that I can draw two ellipses in one function. After creating the design for the eyes, I found I hadn't closed that section which resulted in all of the shapes in my Processing file to be moved around the page. I then closed the section, but now I'm getting a NullPointerException. How do I fix this?

float Helmet_Ratio=.4;
int i=0;

void setup()
{
  size(1280,720);
  smooth();
}

void draw(){
   eye(1200,300);
   eye(1275,350);
   }

   void eye(int y, int z){
   fill(0);
   ellipse(y,z,15,15);
   fill(0);
   ellipse(y+75,z+0,15,15);
}
{
  background(255);  //This is where the problem begins
 {

 //Pattern Background

 int x = 15;
  if (x > 20) { 
  rect(0, 0, 55, 55);
}
if (x < 20) {
  strokeWeight(3);
  fill(0,200,0);
  rect(0,0,25,25);
}

 for (i=0; i<125; i++)
 {
   pushMatrix();
   fill(0,32,32);
   rotate(.75);
   rect(0,i*12,1280,i/5);
   popMatrix();
 }

{

  //Helmet 1 Red
  strokeWeight(0);
  translate(50,0);
  fill(255,0,0);
  ellipse(width/2.25,height/2,240,250);

  //Helmet 1 Lense
  beginShape();

  endShape();

  //Helmet 2 Blue
  strokeWeight(0);
  translate(250,0);
  fill(51,51,255);
  ellipse(width/2.25,height/2,200,195);

  //Helmet 3 Yellow
  strokeWeight(0);
  translate(250,0);
  fill(255,255,0);
  ellipse(width/2.25,height/2,150,160);

  //Helmet 4 Black
  strokeWeight(0);
  translate(-750,0);
  fill(32,32,32);
  ellipse(width/2.25,height/2,200,195);

  //Helmet 5 Pink
  strokeWeight(0);
  translate(-225,0);
  fill(255,102,255);
  ellipse(width/2.25,height/2,150,160);

}
 }

}
Trace
  • 1
  • 1
  • The line marked with "This is where the problem begins" is an [initializer-block](http://stackoverflow.com/questions/3987428/what-is-an-initialization-block). I don't think this is intentional, as the setup is done later (method `setup()`). Just a guess though, since I don't know what library you're using. –  Mar 20 '17 at 08:44
  • 1
    There's no code here that could throw a NPE. – shmosel Mar 20 '17 at 08:46

1 Answers1

0

This is the block causing the problem(with indenting, which I recommend using):

{
  background(255);  //This is where the problem begins
  {

    //Pattern Background

    int x = 15;
    if (x > 20) { 
      rect(0, 0, 55, 55);
    }
    if (x < 20) {
      strokeWeight(3);
      fill(0, 200, 0);
      rect(0, 0, 25, 25);
    }

    for (i=0; i<125; i++)
    {
      pushMatrix();
      fill(0, 32, 32);
      rotate(.75);
      rect(0, i*12, 1280, i/5);
      popMatrix();
    }

    {

      //Helmet 1 Red
      strokeWeight(0);
      translate(50, 0);
      fill(255, 0, 0);
      ellipse(width/2.25, height/2, 240, 250);

      //Helmet 1 Lense
      //beginShape();

      //endShape();

      //Helmet 2 Blue
      strokeWeight(0);
      translate(250, 0);
      fill(51, 51, 255);
      ellipse(width/2.25, height/2, 200, 195);

      //Helmet 3 Yellow
      strokeWeight(0);
      translate(250, 0);
      fill(255, 255, 0);
      ellipse(width/2.25, height/2, 150, 160);

      //Helmet 4 Black
      strokeWeight(0);
      translate(-750, 0);
      fill(32, 32, 32);
      ellipse(width/2.25, height/2, 200, 195);

      //Helmet 5 Pink
      strokeWeight(0);
      translate(-225, 0);
      fill(255, 102, 255);
      ellipse(width/2.25, height/2, 150, 160);
    }
  }
}

Perhaps you meant something to use a function instead of an initialiser block ?

void drawSomeShapes(){
  background(255);  //This is where the problem begins
  {

    //Pattern Background

    int x = 15;
    if (x > 20) { 
      rect(0, 0, 55, 55);
    }
    if (x < 20) {
      strokeWeight(3);
      fill(0, 200, 0);
      rect(0, 0, 25, 25);
    }

    for (i=0; i<125; i++)
    {
      pushMatrix();
      fill(0, 32, 32);
      rotate(.75);
      rect(0, i*12, 1280, i/5);
      popMatrix();
    }

    {

      //Helmet 1 Red
      strokeWeight(0);
      translate(50, 0);
      fill(255, 0, 0);
      ellipse(width/2.25, height/2, 240, 250);

      //Helmet 1 Lense
      //beginShape();

      //endShape();

      //Helmet 2 Blue
      strokeWeight(0);
      translate(250, 0);
      fill(51, 51, 255);
      ellipse(width/2.25, height/2, 200, 195);

      //Helmet 3 Yellow
      strokeWeight(0);
      translate(250, 0);
      fill(255, 255, 0);
      ellipse(width/2.25, height/2, 150, 160);

      //Helmet 4 Black
      strokeWeight(0);
      translate(-750, 0);
      fill(32, 32, 32);
      ellipse(width/2.25, height/2, 200, 195);

      //Helmet 5 Pink
      strokeWeight(0);
      translate(-225, 0);
      fill(255, 102, 255);
      ellipse(width/2.25, height/2, 150, 160);
    }
  }
}

Full listing:

float Helmet_Ratio=.4;
int i=0;

void setup()
{
  size(1280, 720);
  smooth();
}

void draw() {
  eye(1200, 300);
  eye(1275, 350);
  drawSomeShapes();
}

void eye(int y, int z) {
  fill(0);
  ellipse(y, z, 15, 15);
  fill(0);
  ellipse(y+75, z+0, 15, 15);
}
void drawSomeShapes(){
  background(255);  //This is where the problem begins
  {

    //Pattern Background

    int x = 15;
    if (x > 20) { 
      rect(0, 0, 55, 55);
    }
    if (x < 20) {
      strokeWeight(3);
      fill(0, 200, 0);
      rect(0, 0, 25, 25);
    }

    for (i=0; i<125; i++)
    {
      pushMatrix();
      fill(0, 32, 32);
      rotate(.75);
      rect(0, i*12, 1280, i/5);
      popMatrix();
    }

    {

      //Helmet 1 Red
      strokeWeight(0);
      translate(50, 0);
      fill(255, 0, 0);
      ellipse(width/2.25, height/2, 240, 250);

      //Helmet 1 Lense
      //beginShape();

      //endShape();

      //Helmet 2 Blue
      strokeWeight(0);
      translate(250, 0);
      fill(51, 51, 255);
      ellipse(width/2.25, height/2, 200, 195);

      //Helmet 3 Yellow
      strokeWeight(0);
      translate(250, 0);
      fill(255, 255, 0);
      ellipse(width/2.25, height/2, 150, 160);

      //Helmet 4 Black
      strokeWeight(0);
      translate(-750, 0);
      fill(32, 32, 32);
      ellipse(width/2.25, height/2, 200, 195);

      //Helmet 5 Pink
      strokeWeight(0);
      translate(-225, 0);
      fill(255, 102, 255);
      ellipse(width/2.25, height/2, 150, 160);
    }
  }
}
George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • It's an initializer block, not a syntax error. And if it were a syntax error, it couldn't possibly cause a NPE because it wouldn't run. – shmosel Mar 20 '17 at 20:14
  • @shmosel is that why I got down voted (even though I provided a working solution) ? All code inside a Processing sketch is concatenated into a single Java class that extends [PApplet](http://processing.github.io/processing-javadocs/core/processing/core/PApplet.html). If the code wouldn't run, the ```background()``` call wouldn't be executed, and therefore the wouldn't throw ```java.lang.NullPointerException at processing.core.PApplet.background(PApplet.java:14546)``` – George Profenza Mar 20 '17 at 20:54
  • I downvoted for the misinformation. Happy to remove once it's corrected. – shmosel Mar 20 '17 at 20:59
  • Now I've mentioned just "block" and "initialiser block" and I hope that's accurate. I described what appeared to be a syntax error. The user mentioned "create a simple function" in the title, appears to be getting started with code and that was my attempt to point out the cause of the problem and how to fix it, as plainly (not as accurately) as possible. Let me know if the post is ok now. Also, please run the code snippet from the question and and let me know how/why the NPE is caused in a correct language so I may improve. – George Profenza Mar 20 '17 at 21:11