为什么我在此Processing代码中获得nullpointer异常?

时间:2016-12-12 18:16:49

标签: java nullpointerexception processing

我正在使用一个名为Processing(3.2.3)的java程序,我在尝试绘制时得到一个nullpointer。这已经过测试,肯定在rect()调用中。

这是我的主要课程:

square[][] squares;

void setup(){
  frameRate(30);
  size(600, 600, P2D);
  background(0);
  stroke(255);

  int row = 20, col = 20;
  squares = new square[row][col];

  for (int i = 0; i < row; i = i + 30) {
    for (int j = 0; j < col; j = j + 30) {
      squares[i][j] = new square(i, j, i+30, j+30, 1);
    }
  }
}

void draw() {
  frameRate(30);
  for (int i = 0; i < squares.length; i++) {
    for (int j = 0; j < squares[0].length; j++) {
      squares[i][j].display();
    }
  }
}

这是方形物体:

class square {
float x1, y1;
float x2, y2;
int age;

square (float x1, float y1, float x2, float y2, int age) {
  this.x1 = x1;
  this.y1 = y1;
  this.x2 = x2;
  this.y2 = y2;
  this.age = age;
}

void display() {
  stroke(255);
  if (age == 0) {
    fill(0);
  } else {
    fill(255);
  }
  rectMode(CORNERS);
  rect(x1, y1, x2, y2);
}

}

在对rect()的调用中,它给了我一个空指针。

完整错误:

java.lang.NullPointerException
    at sketch_161212a.draw(sketch_161212a.java:38)
    at processing.core.PApplet.handleDraw(PApplet.java:2418)
    at processing.opengl.PSurfaceJOGL$DrawListener.display(PSurfaceJOGL.java:884)
    at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:692)
    at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:674)
    at jogamp.opengl.GLAutoDrawableBase$2.run(GLAutoDrawableBase.java:443)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1293)
    at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1147)
    at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:759)
    at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:81)
    at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:452)
    at com.jogamp.opengl.util.FPSAnimator$MainTask.run(FPSAnimator.java:178)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

0 个答案:

没有答案