We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Image not loading properly, or what
Page Index Toggle Pages: 1
Image not loading properly, or what? (Read 378 times)
Image not loading properly, or what?
Jun 10th, 2009, 7:51am
 
Code:
PImage [] fundo = new PImage [5];
color [][] imagem;

void setup(){
fundo[0] = loadImage ("1.jpg");
fundo[1] = loadImage ("2.jpg");
fundo[2] = loadImage ("3.jpg");
fundo[3] = loadImage ("4.jpg");
fundo[4] = loadImage ("5.jpg");
size(647,400);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
imagem[i][j] = fundo[1].get(i,j); //Here I get a Null Pointer Exception
}
}
}


this is a piece of code I am using to load different images into an array of PImage variables, so then I can copy each of them at a time (by pressing different keys) to a two-dimensional array of color variables.

This two-dimensional array is then used to get colors from pixels of the image based on mouse position.

Anyway, I can't see the problem with this code.
Re: Image not loading properly, or what?
Reply #1 - Jun 10th, 2009, 8:13am
 
I don't know if that's the problem, but I would apply the advice given in the size() reference: "The size() function must be the first line in setup().".

It might help ensure that the data folder is correctly initialized internally, so that the loadImage() calls find the images there (supposing they are actually there, of course).
Re: Image not loading properly, or what?
Reply #2 - Jun 10th, 2009, 8:16am
 
Changing size() to the first line in setup made no difference, I am still getting the NullPointerException at the same line.

But I will keep size to the first line as a good practice.
Re: Image not loading properly, or what?
Reply #3 - Jun 10th, 2009, 8:33am
 
Are the images the same size as your sketch?

Use fundo[1].width and fundo[1].height instead of just width or height.
Those belong to the PApplet instead of the image.

Edit: Just realized your color array has not been initialized.
imagem = new color[width][height];
Re: Image not loading properly, or what?
Reply #4 - Jun 10th, 2009, 8:41am
 
Now i get an Array Index Out of Bounds Exception:0

that's weird because a matrix of size[n] goes from 0 to n-1

right?
Re: Image not loading properly, or what?
Reply #5 - Jun 10th, 2009, 8:49am
 
Be sure to initialize imagem after size().

It ran OK here.
Re: Image not loading properly, or what?
Reply #6 - Jun 10th, 2009, 9:27am
 
now it ran fine, thanks a lot!
Re: Image not loading properly, or what?
Reply #7 - Jun 10th, 2009, 9:36am
 
NoahBuddy, you have a good eye for these little culprits! Wink
Re: Image not loading properly, or what?
Reply #8 - Jun 10th, 2009, 11:39am
 
I've had trouble with most of them myself. Grin
Page Index Toggle Pages: 1