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

2013/2/25

java.lang.nullpointerException-Error

Mepp Mepp

2013/2/25

#
When I compile the code, all is ok, but when i try to run it, it says everytime "java.lang.NullpointerException". Why? it marked "if(mi.getActor()==start){"
danpost danpost

2013/2/25

#
The 'getMouseInfo' method will return 'null' unless there is a mouse action to record. Before trying to 'getActor' from the MouseInfo object, first check to see if the MouseInfo object is 'null'.
Mepp Mepp

2013/2/25

#
I written it this way now:
if(gamestatus==1){
    MouseInfo mi=Greenfoot.getMouseInfo();
    if(mi.getActor()!=null){
        if(mi.getActor()==start) setImage("Clear.png");
    }
}
But it says the same at line 3 now. The same here also:
if(gamestatus==1){
    MouseInfo mi=Greenfoot.getMouseInfo();
    if(mi.getActor()!=null && mi.getActor()==start) setImage("Clear.png");
}
davmac davmac

2013/2/25

#
You're still calling a method on what getMouseInfo returned:
    if(mi.getActor()!=null){  
Here 'mi' might be null, but you're calling getActor() on it. You need to check if mi is null before calling any methods on it:
    if(mi != null){  
There's no need to check if mi.getActor() returns null, because you don't call any methods on that.
Mepp Mepp

2013/2/25

#
Thx, working now! :)
Mepp Mepp

2013/2/25

#
Got another NullpointerException now: The spf.addObject... was marked Just the NullpointerException, the other Errors are cleared.
davmac davmac

2013/2/25

#
That means that 'spf' is null. It is the same problem - you are calling a method on a null reference.
You need to login to post a reply.