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.
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.
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.
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
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