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

2013/4/12

java.lang.NullPointerException

noweh noweh

2013/4/12

#
Hi, I have a problem with my small programm. I'm trying to access a method from another class. I followed this tutorial but I still have this problem. Here is my error message and lines : java.lang.NullPointerException at Bacterie.touchUnder(Bacterie.java:28) at Bacterie.act(Bacterie.java:15) Lines shown :
getWorld().removeObject(this);
            
            Espace3 Espace3World = (Espace3) getWorld();
            eIndicator eindicator = Espace3World.getIndicator(); //this line
            
            eindicator.getEnergy(1);
Here is the code in my Espace3 World :
private eIndicator theIndicator;

//In Espace3 method

theIndicator = new eIndicator();
        addObject(theIndicator, 1050, 640);

//------------------

public eIndicator getIndicator()
    {
        return theIndicator;
    }
And finally the indicator itself (eIndicator.class)
 private int energieRestante;

    public eIndicator()
    {
        setImage("4e.png");
    }
    
    public void act() 
    {
        
    }
    
    public void getEnergy(int howMuch)
    {
        energieRestante += howMuch;
        
        if(energieRestante == 4)
        {
            setImage("4e.png");
        }
        
        else if(energieRestante == 3)
        {
            setImage("3e.png");
        }
        
        else if(energieRestante == 2)
        {
            setImage("2e.png");
        }
        
        else if(energieRestante == 1)
        {
            setImage("1e.png");
        }
        
        else if(energieRestante == 0)
        {
            Greenfoot.stop();
        }
    }
Thank you very much and sorry for my bad english
danpost danpost

2013/4/13

#
In your first code-set given, the first line removes 'this' object from the world and the second, then, gets the world it is in (which now is in no world because of what the first line does). The value of 'Escape3World' is therefore set to 'null' and the third line (where the error occurs) tries to execute a method 'getIndicator' without an object to execute it on (you are without a world object to get an indicator from). If you switch the order of the first two lines in that code-set, you will get the world object before 'getWorld' is set to 'null' by removing 'this' from the world.
noweh noweh

2013/4/13

#
Thanks a lot. It works perfectly
You need to login to post a reply.