1

I am using a java program called Processing (3.2.3), and I am getting a nullpointer when attempting to draw. This has been tested, and is definitely in the rect() call.

Here is my main class:

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

Here is the square object:

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

}

On the call to rect(), it gives me a null pointer.

Full error:

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)
sproga2
  • 11
  • 2
  • because you didn't call `setup`? –  Dec 12 '16 at 18:17
  • setup is called in the main. – sproga2 Dec 12 '16 at 18:27
  • And this is definitely not a duplicate. I have gone through many explanations like the one linked to, and nothing seems to be wrong here. – sproga2 Dec 12 '16 at 18:28
  • In other words, from what i have found in the past 2 hours, there is no reason for this to be nullpointer. The same issue happens when I draw in the draw method, and not in the square class. – sproga2 Dec 12 '16 at 18:29
  • in setup: `i = i + 30` and in draw: `i++` that might be a hint. Regarding the duplicate, you get a NullPointerException, and you want to fix it, no? –  Dec 12 '16 at 18:33
  • Ignoring the whole duplicate thing, I AM AN IDIOT. Thanks! I forgot that when I first started making this, I had the loops constructed slightly differently. – sproga2 Dec 12 '16 at 18:41
  • for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++){ squares[i][j] = new square(30 * i, 30 * j, (30 * i) + 30, (30 * j) + 30, 1); } } } – sproga2 Dec 12 '16 at 18:41
  • That works perfectly. Thank you so much for your help! – sproga2 Dec 12 '16 at 18:42
  • @RC Please note that this is a Processing question, and [Processing != Java](http://meta.stackoverflow.com/questions/321127/processing-java). Specifically, the `setup()` function is automatically called by Processing, a bit like `main()` is automatically called in Java. – Kevin Workman Dec 22 '16 at 05:41
  • Please read all the comments.. and note the "?" after "setup" in my first comment –  Dec 22 '16 at 08:00

0 Answers0