NullPointedException

When I run the program it says NullPointedException… could someone help me see what’s wrong? Sorry if it’s a bit disorganised…

import processing.video.*;

Capture video;
PImage prev;
ArrayList<Circle> circles;
ArrayList<PVector> spots;
PImage img;


float threshold = 25;

float motionX = 0;
float motionY = 0;



void setup() {
  size(800,400);
  spots= new ArrayList<PVector>();
  img=loadImage("primavera.png");
  img.loadPixels();
  for (int x = 0; x < img.width; x++) {
    for(int y = 0; y < img.height; y++) {
      int index = x + y * img.width;
      color c =img.pixels[index];
      float b = brightness(c);
      if (b > 1) {
        spots.add(new PVector(x,y));
    }
    }}
  String[] cameras = Capture.list();
  printArray(cameras);
  video = new Capture(this, cameras[3]);
  video.start();
  prev = createImage(640, 360, RGB);

}


void captureEvent(Capture video) {
  prev.copy(video, 0, 0, video.width, video.height, 0, 0, prev.width, prev.height);
  prev.updatePixels();
  video.read();
}

void draw() {
  video.loadPixels();
  prev.loadPixels();
  image(video, 0, 0);

  //threshold = map(mouseX, 0, width, 0, 100);
  threshold = 50;


  int countt = 0;
  float avgX = 0;
  float avgY = 0;
  int total = 10;
  int count = 0;
  int attempts = 0;
 
  loadPixels();
  
  while (count <  total) {
    Circle newC = newCircle();
    if (newC != null) {
      circles.add(newC);
      count++;
    }
    attempts++;
    if (attempts > 1000) {
      noLoop();
      println("FINISHED");
      break;
    } 
  }
  for (Circle c : circles) {
    if (c.growing) {
      if (c.edges()) {
        c.growing = false;
      } else {
        for (Circle other : circles) {
          if (c != other) {
            float d = dist(c.x, c.y, other.x, other.y);
            if (d - 2 < c.r + other.r) {
              c.growing = false;
              break;
            }
          }
        }
      }
    }
    c.show();
    c.grow();
  }
  // Begin loop to walk through every pixel
  for (int x = 0; x < video.width; x++ ) {
    for (int y = 0; y < video.height; y++ ) {
      int loc = x + y * video.width;
      // What is current color
      color currentColor = video.pixels[loc];
      float r1 = red(currentColor);
      float g1 = green(currentColor);
      float b1 = blue(currentColor);
      color prevColor = prev.pixels[loc];
      float r2 = red(prevColor);
      float g2 = green(prevColor);
      float b2 = blue(prevColor);

      float d = dist(r1, g1, b1, r2, g2, b2); 

      if (d > threshold*threshold) {
        //stroke(255);
        //strokeWeight(1);
        //point(x, y);
        avgX += x;
        avgY += y;
        countt++;
        pixels[loc] = color(255);
      } else {
        pixels[loc] = color(0);
      }
    }
  }
  updatePixels();

  // We only consider the color found if its color distance is less than 10. 
  // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
  if (count > 200) { 
    motionX = avgX / count;
    motionY = avgY / count;
  }
  
  //float distSq(float x1, float y1, float z1, float x2, float y2, float z2) {
  //float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
  //return d;
}

Circle newCircle() {
  int r = int(random(0,spots.size()));
  PVector spot = spots.get(r);

  float x = spot.x;
  float y = spot.y;

  boolean valid = true;
  for (Circle c : circles) {
    float d = dist(x, y, c.x, c.y);
    if (d < c.r) {
      valid = false;
      break;
    }
  }

  if (valid) {
    return new Circle(x, y);
  } else {
    return null;
  }
}
1 Like

Which line is it complaining about?

By the way, you should get into the habit of testing your code in small chunks. Don’t try to write your whole program and then test it after you’re done with the whole thing. Instead, write just a couple of lines at a time, and test that those couple of lines work the way you expected. That way you’ll know where the problem is when you get an exception and you can post a MCVE instead of your whole program, which will make it much easier to help you.

Sorry, I’m really new to this!
The program highlights this line (almost at the end of the code):
for (Circle c : circles) {

That makes me think your circles variable is null, which means you haven’t set it to anything. And doing a quick search through your code, I don’t see you set its value anywhere.

I think you want to do something like this:

circles = new ArrayList<Circle>();

You might be able to do this right when you declare the variable, like this:

ArrayList<Circle> circles = new ArrayList<Circle>();

But it really depends on how you’re using it.

1 Like