# NullPointerExeption when using img.get()

**URL:** <https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872>\
**Category:** Coding Questions\
**Created:** [February 16, 2020, 7:50pm UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872 "2020-02-16T19:50:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parzivalius](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@Parzivalius](https://discourse.processing.org/u/Parzivalius)\
**Post date:** [February 16, 2020, 7:50pm UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872/1 "2020-02-16T19:50:14Z")

</div>

Hey, I am trying to get the values of a pixel in an image as well as the values of the pixels around it, however the get() function is returning a null pointer exception.  
Most likely something wrong in my code:

```auto
void feedForward(String[] images){
  img = loadImage(images[int(random(images.length))], "png"); //Gets a random image from the images[] array
  img.resize(img.width, img.height); //Makes sure that the image is the original size
  image(img, 0, 0);
  for(int y = 0; y <= img.height; y++){
    for(int x = 0; x <= img.width; x++){
        desired[0] = img.get(x,y);
        desired[1] = img.get(x+1, y);
        desired[2] = img.get(x,y+1);
        desired[3] = img.get(x+1, y+1); 
        desired[4] = img.get(x-1, y);
        desired[5] = img.get(x, y-1);
        desired[6] = img.get(x-1, y-1);
        desired[7] = img.get(x-1, y+1);
        desired[8] = img.get(x+1, y-1);
    }
  }
//The rest of the funtction is irrelevant

```

Any help or pointers would be appreciated, I have been pulling my hair out on this one for the past week, thanks.

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [February 16, 2020, 8:54pm UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872/2 "2020-02-16T20:54:44Z")

</div>

Try this next, three changes per line:

```auto
for(int y = 1; y < img.height-1; y++){
    for(int x = 1; x < img.width-1; x++){

```

The important thing here is how to debug this code. A simple mental substitution of x to zero shows that you get a negative index which `get()` will not know how to handle and simply return null. You either manage the null or re-design the algorithm so restricts your calculation within a valid are of your image.

Btw, this is redundant: `img.resize(img.width, img.height);` aka. you don’t need this.

Kf

---

<div class="post-metadata">

**Author:** ![Parzivalius](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@Parzivalius](https://discourse.processing.org/u/Parzivalius)\
**Post date:** [February 17, 2020, 4:47am UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872/3 "2020-02-17T04:47:42Z")

</div>

Hey thanks for the help. I made the changes and debugged with that in mind however I am still getting the null pointer. I haven’t quite had time to fully delve into the issue and I will get to that soon, so I may of missed something.  
Also thanks for the pointer on the img.resize() I wasn’t sure if this was required or not.

---

<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:** [February 17, 2020, 8:49am UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872/4 "2020-02-17T08:49:36Z")

</div>

You could start the for loops at 1 and let them go to image width and height -1

You could write your own function as a wrapper that checks whether it’s \<0 before executing get()

Also in which line does the error occur exactly?

---

<div class="post-metadata">

**Author:** ![Parzivalius](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@Parzivalius](https://discourse.processing.org/u/Parzivalius)\
**Post date:** [February 17, 2020, 7:48pm UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872/5 "2020-02-17T19:48:43Z")

</div>

Those seem like good ideas, I will give them a go.  
This error is very annoying because it only says:  
“NullPointerExeption”  
I would like for it to say more, but sadly that’s all it tells

---

<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:** [February 17, 2020, 7:52pm UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872/6 "2020-02-17T19:52:45Z")

</div>

Interesting.

But does it mark exact line? In yellow or red?

---

<div class="post-metadata">

**Author:** ![Parzivalius](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@Parzivalius](https://discourse.processing.org/u/Parzivalius)\
**Post date:** [February 17, 2020, 9:09pm UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872/7 "2020-02-17T21:09:13Z")

</div>

Yes I forgot to mention that, the first desired[0] = img.get(x,y); highlights red, if I comment out this line the next is highlighted

---

<div class="post-metadata">

**Author:** ![Parzivalius](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@Parzivalius](https://discourse.processing.org/u/Parzivalius)\
**Post date:** [February 17, 2020, 9:10pm UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872/8 "2020-02-17T21:10:28Z")

</div>

\*\*Highlights Yellow  
Sorry the error is red, the line highlights yellow

---

<div class="post-metadata">

**Author:** ![Parzivalius](https://avatars.discourse-cdn.com/v4/letter/p/838e76/32.png) [@Parzivalius](https://discourse.processing.org/u/Parzivalius)\
**Post date:** [February 18, 2020, 5:39am UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872/9 "2020-02-18T05:39:38Z")

</div>

Thank you for all the help, after some mucking around I found that the solution was stupidly simple. As it always tends to be.

The reason for the error was that I had declared the desired[] array as such:

```auto
float[] desired;

```

This caused the array to not have any data and therefore being Null the way I solved this was by filling the array with the value 0. The end result looked like this:

```auto
float[] desired = {0, 0, 0, 0, 0, 0, 0, 0, 0}; 

void feedForward(String[] images){
  img = loadImage(images[int(random(images.length))], "png"); //Gets a random image from the images[] array
  image(img, 0, 0);
  for(int y = 1; y <= img.height -1; y++){
    for(int x = 1; x <= img.width -1; x++){
        desired[0] = img.get(x,y);
        desired[1] = img.get(x+1, y);
        desired[2] = img.get(x,y+1);
        desired[3] = img.get(x+1, y+1); 
        desired[4] = img.get(x-1, y);
        desired[5] = img.get(x, y-1);
        desired[6] = img.get(x-1, y-1);
        desired[7] = img.get(x-1, y+1);
        desired[8] = img.get(x+1, y-1);
    }
  }
//The rest of the funtction is irrelevant

```

Thank you for the help

---

<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:** [February 18, 2020, 8:22am UTC](https://discourse.processing.org/t/nullpointerexeption-when-using-img-get/17872/10 "2020-02-18T08:22:23Z")

</div>

Ah, okay. You forgot to initial the array

We could find something like this out by using println before that line
