0
votes

I'm using Processing 2.0b7. I have a Spool class that is supposed to have an ArrayList of Note objects in it. In Spool's draw method, I want to call each Note in the ArrayList's draw method. However, when I try do that with this syntax, I get the error "

Exception in thread "Animation Thread" java.lang.NullPointerException at spoolloops$Spool.draw(spoolloops.java:119) at spoolloops.draw(spoolloops.java:39) at processing.core.PApplet.handleDraw(PApplet.java:2142) at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:193) at processing.core.PApplet.run(PApplet.java:2020) at java.lang.Thread.run(Thread.java:662)

PLEASE NOTE This is the Processing environment, which runs some sort of Java, but is different in important respects. I know the syntax is very different, because you don't have to declare scope or return types for methods in classes. But since I'm not a Java export, I don't know the exact hows and whyfors of the differences.

If you want to give an answer, please make sure that it will work in processing and not just Java. I'm pretty sure that this code will throw all kinds of errors in a pure Java environment, but that's not what it's running in, so it doesn't matter. The platform is Processing.

class Spool {

  int diameter = 50;
  int i, angle;
  Note note, test;

  ArrayList<Note> notes;

  void Spool() {
    notes = new ArrayList<Note>();
    notes.add( note = new Note(100.0,100.0,100.0,100.0,100.0) );
    notes.add( note = new Note(120.0,120.0,120.0,120.0,120.0) );
  }

  void draw() {    
    for (int i = 0; i < notes.size(); i++) {
      test = (Note)notes.get(i);
      test.draw();

    }

    angle = angle + 1;
    if ( angle > 360 ) {
      angle = 0;
    }

  }
}

class Note {

  float diameter, x,y, start, stop;

  Note(float Diameter, float X, float Y, float Start, float Stop) {
    diameter = Diameter;
    x = X;
    y = Y;
    start = Start;
    stop = Stop;
  }

  void turn(float degrees) {
  }

  void draw() {
    strokeWeight(25);
    arc( x,y,diameter, diameter, radians(start), radians(stop));
  }
}

The notes.size() in the for loop is what seems to cause the problem, but when I change it to i < 1, the error occurs at test = (Note)notes.get(i);. I suppose the ArrayList is not properly getting filled with Note Objects?

1

1 Answers

2
votes

Class and Object Initializers do not return a type. Remove the "void" from

void Spool()

leave it as

Spool()

Otherwise it is not called during creation.

Also it's not totally necessary to have the

notes.add( note = new Note(100.0,100.0,100.0,100.0,100.0) );

It is my understanding that it can simply be:

notes.add(new Note(100.0,100.0,100.0,100.0,100.0) );