# Adding an Array but gets NullPointException

**URL:** <https://discourse.processing.org/t/adding-an-array-but-gets-nullpointexception/14595>\
**Category:** Libraries\
**Created:** [October 11, 2019, 2:52am UTC](https://discourse.processing.org/t/adding-an-array-but-gets-nullpointexception/14595 "2019-10-11T02:52:02Z")\
**Posts on this page:** 2\
**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 11, 2019, 2:52am UTC](https://discourse.processing.org/t/adding-an-array-but-gets-nullpointexception/14595/1 "2019-10-11T02:52:03Z")

</div>

Hi, so im working on a code where it will track a users face through the OpenCV library. At the moment the code works with a single image and will trace the users face. However when i add an Array to it to add a element of randomisation to the code it give me the Null Point Error. If you could take a look that be amazing.

```auto
import gab.opencv.*;
import processing.video.*;
import java.awt.*;
//PImage img;
PImage [] imgArray = new PImage[5]; //image array
//PImage bg;

Capture video;
OpenCV opencv;

void setup() {
size(640, 480);

for (int i=1;i<imgArray.length; i++){ //Setting up Array (randomiser)
  imgArray[i]=loadImage("photo" + i +".png");
}
  //imgBack = loadImage("sea.png"); //background
  
  //img = loadImage("photo1.png");
  //img = loadImage ("photo2.png");
  //img = loadImage("photo3.png");
  //img = loadImage ("photo4.png");
  //img = loadImage ("photo5.png");
  
  //bg=loadImage ("sea.png");
  
  video = new Capture(this, 640, 480);
  opencv = new OpenCV(this, 640, 480);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  

  video.start();
}

void draw() {
  //background (bg);
  scale(2);
  opencv.loadImage(video);

  //image(imgBack,0,0);
  //image(bg,0,0);
  image(video, 0, 0 );

  noFill();
  stroke(0, 0, 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);
    //rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
    //image(img, faces[i].x, faces[i].y, faces[i].width,faces[i].height);
    image(imgArray[i],faces[i].x,faces[i].y,faces[i].width,faces[i].height); //Calling Array Image
  }
}

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

//void mousePressed(){
  //img(imgArray[(int)random(25)],0,0,width,height);

//}

```

---

<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 11, 2019, 3:48am UTC](https://discourse.processing.org/t/adding-an-array-but-gets-nullpointexception/14595/2 "2019-10-11T03:48:32Z")

</div>

The for loop in setup must start with 0 not with 1
