Why am I getting NullPointerException?

edited January 2015 in Questions about Code

And here I am again, with a code that is not working . I am trying to run this but I keep getting NullPointerException. I guess it is a problem with declaring the array.

Bubble[] myBubble = new Bubble[20];
    void setup() {
      size(500, 500); 
      for (int i=0; i< myBubble.length; i++) {
        int s = 20;
        int x1 = int(random(20, 480)); 
        int y1 = int(random(20, 480)); 
        int z = int(random(3)); 
        color c1; 
        if ( z>=1) {
          c1 = color(255, 0, 0);
        } else {
          c1= color(0, 0, 255);
        } 
        myBubble [i] = new Bubble( x1, y1, c1, s);
        for (int j=0; j< myBubble.length; j++) {
          if (dist(myBubble[i].x, myBubble [i].y, myBubble[j].x, myBubble [j].y)< myBubble[i].size/2 ||
            dist(myBubble[i].x, myBubble [i].y, myBubble[j].x, myBubble [j].y)< myBubble[j].size/2) {
            myBubble[i].x = int(random(20, 480));
            myBubble[i].y = int(random(20, 480));
          }
        }
      }
    }
    void draw() {
      background(255);
      for (int i = 0; i<myBubble.length; i++) {
        myBubble [i] .display();
      }
    }

Answers

Sign In or Register to comment.