# NullPointerException when after 2 secconds

**URL:** <https://discourse.processing.org/t/nullpointerexception-when-after-2-secconds/14871>\
**Category:** Libraries\
**Created:** [October 22, 2019, 12:46pm UTC](https://discourse.processing.org/t/nullpointerexception-when-after-2-secconds/14871 "2019-10-22T12:46:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DatSoi](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@DatSoi](https://discourse.processing.org/u/DatSoi)\
**Post date:** [October 22, 2019, 12:46pm UTC](https://discourse.processing.org/t/nullpointerexception-when-after-2-secconds/14871/1 "2019-10-22T12:46:20Z")

</div>

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

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2019, 3:52pm UTC](https://discourse.processing.org/t/nullpointerexception-when-after-2-secconds/14871/2 "2019-10-22T15:52:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DatSoi](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@DatSoi](https://discourse.processing.org/u/DatSoi)\
**Post date:** [October 22, 2019, 7:55pm UTC](https://discourse.processing.org/t/nullpointerexception-when-after-2-secconds/14871/3 "2019-10-22T19:55:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 22, 2019, 8:22pm UTC](https://discourse.processing.org/t/nullpointerexception-when-after-2-secconds/14871/4 "2019-10-22T20:22:17Z")

</div>

What does print(i); give you

---

<div class="post-metadata">

**Author:** ![Waboqueox](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/waboqueox/32/5643_2.png) [@Waboqueox](https://discourse.processing.org/u/Waboqueox)\
**Post date:** [October 23, 2019, 7:57am UTC](https://discourse.processing.org/t/nullpointerexception-when-after-2-secconds/14871/5 "2019-10-23T07:57:57Z")

</div>

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

```auto
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 😅😅… and probably there is a more elegant way to do it controlling the indexes 😉.

Hope you find your solution soon 🤗
