This site requires JavaScript, please enable it in your browser!
Greenfoot back
Ragtime
Ragtime wrote ...

2013/3/29

NullPointerException, help please

Ragtime Ragtime

2013/3/29

#
Hi, I'm new to programming and I'm trying to use the following code in order to save several images in a vector from which later a random image is called. GreenfootImage img; public void setimg() { img= new GreenfootImage("30.gif"); img= new GreenfootImage("uev.gif"); } When I compile, the error NullPointerException occurs at setimg(), could anyone help me with this please?
JetLennit JetLennit

2013/3/29

#
what does the img mean?
Ragtime Ragtime

2013/3/29

#
I think it means position 0/1 in the vector img. As I said, i'm new to programming.
JetLennit JetLennit

2013/3/29

#
i may not be the best to help you with this (i dont understand vector images)... but if your new to programming.. why are you using vector stuff?
Ragtime Ragtime

2013/3/29

#
Well the only way to learn something is to try new things...
Gevater_Tod4711 Gevater_Tod4711

2013/3/29

#
I don't realy know what you mean by vector. The stuff that looks like this: GreenfootImage is called Array. An Array is a variable that saves many objects from one type in only one variable. In your case the problem is that you try to set the object at the first and second field of the array which doesn't even exist. To fix it you have to change this line: GreenfootImage img; into this: GreenfootImage img = new GreenfootImage;//or a higher value if you want to use more images.
Ragtime Ragtime

2013/3/29

#
Thank you very much. I wrote vector because that's how people use to say in my language. Didn't know that Array is the correct term. :)
You need to login to post a reply.