for loop

hello,why this for loop is giving me null pointer exception? Before Setup

PVector PsVerts[] ;
PVector PsVerts2[]; 

In draw

pushMatrix();
  rotateY(-k); //-----------------HERE
  translate(Mouse.x, Mouse.y, Mouse2.z+width/2);
  rotateY(k); //
  fill(26, 59, 45);
  beginShape();

  for (int i=0; i < 800; i+=10 ) {
    println(i);
    PVector PsVerts[] = new PVector [i];
PVector PsVerts2[] = new PVector [i];
println(i);
   PsVerts[i].x = Mouse.x;
    PsVerts[i].y = Mouse.y ;

    PsVerts2[i].x = Mouse2.x;
    PsVerts2[i].y = Mouse2.y ;

    vertex(Mouse.x, Mouse.y, 0);
    vertex(Mouse2.x, Mouse2.y, 0);
  }
  endShape();
  popMatrix();

Answers

  • Answer ✓

    that's not how you use new()

    if you know the number of elements beforehand (800 by the look) than use it in the global definition

    static final int VECS = 800; // constant
    PVector psVerts[] = new PVector[VECS];
    PVector psVerts2[] = new PVector[VECS];
    

    and then you have to initialise each of those before you use it

    (you can assign values in the new(), save having to set them later)

    for (int i = 0  ; i < VECS ; i += 10) {
      psVerts[i] = new PVector(mouse.x, mouse.y);
      psVerts2[i] = new PVector(mouse2.x, mouse2.y);
    }
    

    by tradition variables in java have initialLowerCaseNames, classes have InitialUpperCaseNames, constants are ALL_UPPER_CASE.

  • Answer ✓

    Reference: https://processing.org/reference/Array.html

    the above is basically the 3rd example there.

Sign In or Register to comment.