试图创造一个简单的功能,它的背叛;空指针异常

时间:2017-03-20 08:41:15

标签: java processing

所以我正在创建一个代码。在其中,我想为头盔制作两只眼睛。要做到这一点,我需要创建一个函数,以便我可以在一个函数中绘制两个椭圆。在为眼睛创建设计之后,我发现我没有关闭该部分,这导致我的Processing文件中的所有形状都在页面上移动。然后我关闭了该部分,但现在我得到了一个N​​ullPointerException。我该如何解决这个问题?

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);

}
 }

}

1 个答案:

答案 0 :(得分:0)

这是导致问题的阻塞(我建议使用缩进):

{
  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);
    }
  }
}

也许您的意思是使用函数而不是初始化块?

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);
    }
  }
}

完整列表:

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);
    }
  }
}