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

2017/1/13

`java.lang.NullPointerException` error

dandrei279 dandrei279

2017/1/13

#
While working on my project I faced `java.lang.NullPointerException` error. This is my code of prepare method of class 'Space' that extends `World`:
 public void prepare()
    {
        Rocket rocket = new Rocket(); //here is the error
        addObject(rocket,320,310);
        venus = new Venus();
        addObject(venus,110,310);
        terra = new Terra();
        addObject(terra,320,310);
        moon = new Moon();
        addObject(moon,360,170);
        mars = new Mars();
        addObject(mars,535,310);
        jupiter = new Jupiter();
        addObject(jupiter,800,310);
        Help help=new Help();
        addObject(help, 850, 25);
        soil = new Soil();
        addObject(soil, 450, 30);
    }
'venus', 'moon' and the other objects were declared earlier. The `Rocket` class looks like this:
public class Rocket extends Actor
{
    Space a=(Space) getWorld();
    Venus venus=a.getVenus(); //here is the error
    Mars mars=a.getMars();
    Moon moon=a.getMoon();
    Jupiter jupiter=a.getJupiter();
    Terra terra=a.getTerra();
    /*other lines that work fine*/
}
The method `getVenus` is defined in 'Space' class and looks like this:
public Venus getVenus() {
        return venus;
}
Super_Hippo Super_Hippo

2017/1/13

#
The code is executed when the object is created, so before it is added to the world. 'getWorld' returns null and trying to call a method on null gives you the nullpointer error.
dandrei279 dandrei279

2017/1/13

#
What can I do to fix it?
Super_Hippo Super_Hippo

2017/1/13

#
You could create the fields and set them to the desired objects after the rocket is added to the world (you can use the 'addedToWorld' method). However, I think you don't need to save a reference to all these objects in your rocket class.
dandrei279 dandrei279

2017/1/16

#
After 2 days of trying, I can't get rid of this error. Can you be more explicit about this 'addedToWorld' method?
danpost danpost

2017/1/16

#
dandrei279 wrote...
After 2 days of trying, I can't get rid of this error. Can you be more explicit about this 'addedToWorld' method?
In the Rocket class, do this:
// change these lines:
public class Rocket extends Actor
{
    Space a=(Space) getWorld();


// to this:
protected void addedToWorld(World world)
{
    Space a=(Space)world;
dandrei279 dandrei279

2017/1/18

#
I changed the lines and now I get 'class, interface, or enum expected' error at `void`
danpost danpost

2017/1/18

#
dandrei279 wrote...
I changed the lines and now I get 'class, interface, or enum expected' error at `void`
Please show the Rocket class code. You probably just messed up on bracketing.
dandrei279 dandrei279

2017/1/18

#
I fixed it! I moved the 'Space a=(Space) getWorld();' line in act() when the user presses `SPACE`. Now everything works fine! Thanks Super_Hippo! Thanks danpost!
You need to login to post a reply.