NullPointerExeption when using img.get()

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:

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.

1 Like

Try this next, three changes per line:

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

2 Likes

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.

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?

1 Like

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

Interesting.

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

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

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

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:

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:

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

2 Likes

Ah, okay. You forgot to initial the array

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