NullPointerException when after 2 secconds

Hi so my code seems to give me NullPointerException when i get to the 86th line. Im not sure why could you help.

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

int maxImages = 61; //61 Frames
int imageIndex = 0;  
PImage [] imgArray = new PImage[maxImages]; //image array

PImage background;
PImage imgCoral;
color colCoral;

Capture video;
OpenCV opencv;

void setup() {
size(960, 540);
colorMode (HSB);
   
  background=loadImage ("sea1.png");
  imgCoral = loadImage ("coral1.png");
        
  video = new Capture(this, 640, 480);
  opencv = new OpenCV(this, 640, 480);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE); 
  
  video.start();

     for (int i=0;i<imgArray.length; i++){ //Setting up Array (randomiser)
      imgArray[i]=loadImage("clown" + i +".png");
    }
   
  frameRate(60);
  
}

void draw() {
  scale(1);
  opencv.loadImage(video);
  background (background);
  image(imgCoral,0,0);
   
  noFill();
  stroke(0, 255, 0);
  strokeWeight(3);
  Rectangle[] faces = opencv.detect();
  println(faces.length);

  for (int i = 0; i < faces.length; i++) {
    println(faces[i].x + "," + faces[i].y);
    image(imgArray[imageIndex],faces[i].x,faces[i].y,faces[i].width,faces[i].height); //Calling Array Image
        if (faces[i].x>=180 && faces[i].y<=100){
         noTint();
         image(imgArray[imageIndex],faces[i].x,faces[i].y, faces[i].width,faces[i].height);
         imageIndex = (imageIndex+1)%imgArray.length;
         tint(0,153,204,100);
         image(imgCoral,0,0);
         } 
         else {
           noTint();
           image(imgCoral,0,0);
           
         }
    }
    
    imageIndex=(imageIndex+1)%imgArray.length;
  }


void captureEvent(Capture c) {
  c.read();
}
1 Like

Which line? I don’t think there are so many lines as 86…

oh sorry, its the first:

“image(imgArray[imageIndex],faces[i].x,faces[i].y, faces[i].width,faces[i].height);”

that appeares in the void draw

What does print(i); give you

1 Like

:thinking: mmm in order to avoid de error, maybe you can add :

if(imageIndex<imgArray.length)
      image(imgArray[imageIndex],faces[i].x,faces[i].y,faces[i].width,faces[i].height); 

Before use image();

Don’t know if that will fix your problem because I can’t try your code :sweat_smile::sweat_smile:… and probably there is a more elegant way to do it controlling the indexes :wink:.

Hope you find your solution soon :hugs:

1 Like