Getting a NullPointerException with opencv loadImage()!

I need someone more skilled than me to look at my code. I am trying to open my webcam, and make it track a location, where it will draw a circle. However, it wont even load the cam! I get a NullPointerException. I see there must be an easy solution, but I cannot find it.

import processing.video.*;
import gab.opencv.*;

Capture cam;
OpenCV opencv;
PVector prevLoc;

void setup() {
  size(1920, 1080);
  //background(255);
  noStroke();
  
  String[] cameras = Capture.list();
  
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }
    cam = new Capture(this, 1920, 1080, cameras[1]);
    cam.start(); 
    prevLoc = new PVector(0, width/2);
    frameRate(24);
  }
}

void captureEvent(Capture cam) {
  if (cam.available() == true) {
    cam.read();
  }
  image(cam, 0, 0);
}

void draw() {
  opencv.loadImage(cam); //Here I get the NullPointerException error
  PVector loc = opencv.min();
  ellipse(loc.x, loc.y, 50, 50);
  prevLoc = loc;
}

void keyPressed() {
  if (key == ' ') {
    background(255);
  }
}

same issue with the other thread - please post link to other forums https://www.reddit.com/r/processing/comments/p5bydt/nullpointerexception_error_with_loadimage_using_a/
(and it’s answered on reddit already)

1 Like