Java 试图创建一个简单的函数,它';反作用;空指针异常

Java 试图创建一个简单的函数,它';反作用;空指针异常,java,processing,Java,Processing,所以我正在创建一个代码。在里面,我想为头盔做两只眼睛。为此,我需要创建一个函数,以便在一个函数中绘制两个椭圆。在为眼睛创建设计之后,我发现我没有关闭该部分,这导致处理文件中的所有形状都在页面上移动。然后我关闭了这个部分,但是现在我得到了一个NullPointerException。我该如何解决这个问题 float Helmet_Ratio=.4; int i=0; void setup() { size(1280,720); smooth(); } void draw(){ e

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

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

}
 }

}
float\u比率=.4;
int i=0;
无效设置()
{
规模(1280720);
光滑的();
}
作废提款(){
眼睛(1200300);
眼(1275350);
}
空心眼(内y、内z){
填充(0);
椭圆(y,z,15,15);
填充(0);
椭圆(y+75,z+0,15,15);
}
{
背景(255);//这就是问题的开始
{
//图案背景
int x=15;
如果(x>20){
rect(0,0,55,55);
}
如果(x<20){
冲程重量(3);
填充(0200,0);
rect(0,0,25,25);
}

对于(i=0;i这是导致问题的块(我建议使用缩进):

{
背景(255);//这就是问题的开始
{
//图案背景
int x=15;
如果(x>20){
rect(0,0,55,55);
}
如果(x<20){
冲程重量(3);
填充(0,200,0);
rect(0,0,25,25);
}
对于(i=0;i20){
rect(0,0,55,55);
}
如果(x<20){
冲程重量(3);
填充(0,200,0);
rect(0,0,25,25);
}
对于(i=0;i20){
rect(0,0,55,55);
}
如果(x<20){
冲程重量(3);
填充(0,200,0);
rect(0,0,25,25);
}

对于(i=0;标有“这是问题开始的地方”的行是一个。我不认为这是故意的,因为稍后会进行设置(方法
setup()
)。不过我只是猜测一下,因为我不知道您使用的是什么库。这里没有可以抛出NPE的代码。这是一个初始值设定项块,不是语法错误。如果是语法错误,它不可能导致NPE,因为它不会运行。@shmosel是我被否决的原因(尽管我提供了一个有效的解决方案)?处理草图中的所有代码都连接到一个扩展的Java类中。如果代码不运行,则不会执行
background()
调用,因此不会在Processing.core.PApplet.background(PApplet.Java:14546)处抛出
Java.lang.NullPointerException
我对错误信息投了反对票。更正后很高兴删除。现在我只提到了“块”和“初始化器块”,我希望这是正确的。我描述了一个语法错误。用户提到“创建一个简单函数”在标题中,似乎是开始使用代码,这是我试图指出问题的原因以及如何解决它的原因,非常简单(不那么准确)尽可能。让我知道这篇文章现在是否可以。另外,请运行问题中的代码片段,并让我知道NPE是如何/为什么用正确的语言造成的,以便我可以改进。
{
  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);
    }
  }
}