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

2013/9/3

Need help - nullPointerException

1
2
mukkainaresh mukkainaresh

2013/9/3

#
Hi, I am trying to make an actor appear in the world when i perform an action. I get this nulPointerException when I try it. I have tried calling a method that will add the actor (addObject(..., ..., ...) present in the world class. I have tried world -> WorldClass public void addActorToWorld(){ Actor actor = new Actor(); addObject(actor, 300,460); } Actor class . . . ((WorldClass) getWorld()).addActorToWorld(); //source of nullPointerException }
Zamoht Zamoht

2013/9/3

#
If the actor is not in the world when you call "((WorldClass) getWorld()).addActorToWorld();" the actor has no reference to the world and getWorld() will return null. If the actor is in the world please tell me.
You are trying to tell the Actor to add itself to a world that doesn't exist. The world doesn't exist, because the actor isn't in one. You have to add the actor, using the WorldClass, not the Actor class.
danpost danpost

2013/9/3

#
I believe you are trying to add an object of Actor type (not sub-classed to a specific type of actor). You need to create a sub-class of Actor and give it a name (like 'Wombat') before you can add an Actor of type Wombat into the world. To subclass the Actor class, right click on the Actor icon in the class structure section on the right and select 'New subclass...', fill in the information and click on the 'Ok' button. Then you will be able to add an actor of that type into the world.
mukkainaresh mukkainaresh

2013/9/3

#
Zamoht wrote...
If the actor is not in the world when you call "((WorldClass) getWorld()).addActorToWorld();" the actor has no reference to the world and getWorld() will return null. If the actor is in the world please tell me.
FlyingRabidUnicornPig wrote...
You are trying to tell the Actor to add itself to a world that doesn't exist. The world doesn't exist, because the actor isn't in one. You have to add the actor, using the WorldClass, not the Actor class.
Actor is already there in the world but in a different x,y co-ordinate.
danpost wrote...
I believe you are trying to add an object of Actor type (not sub-classed to a specific type of actor). You need to create a sub-class of Actor and give it a name (like 'Wombat') before you can add an Actor of type Wombat into the world.
I am calling the ((WorldClass) getWorld()).addActorToWorld(); from a actor subclass.
mukkainaresh mukkainaresh

2013/9/3

#
Zamoht wrote...
If the actor is not in the world when you call "((WorldClass) getWorld()).addActorToWorld();" the actor has no reference to the world and getWorld() will return null. If the actor is in the world please tell me.
Lets say, the actor is not in the world, how do I get the actor into the world when a action is performed by another actor? I am still trying to get used to greenfoot.
mukkainaresh mukkainaresh

2013/9/3

#
My World class public class BallWorld extends World { public BallWorld() { super(800, 600, 1); setPaintOrder(Coin.class, Alien.class, ball.class, BallMachine.class); prepare(); } private void prepare() { BallMachine ballmachine = new BallMachine(); addObject(ballmachine, 367, 237); addObject( new Penny(), 57, 71 ) ; addObject( new Quarter(), 68, 156 ) ; addObject( new FakeQuarter(), 66, 248 ) ; addObject(new Inspector(), 533, 291); addObject(new RandomPicker(), 655, 94); addObject(new GreenPicker(), 669, 456); } public void dispRed(){ addObject(new RedBall(), 366, 380); } } My Actor class public class RandomPicker extends Picker { int color = 0; /** * Act - do whatever the RandomPicker wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void pop(){ color = 2; //Greenfoot.getRandomNumber(3); eject(color); } public void eject(int ballColor){ if(ballColor == 1){ // future code } else if(ballColor == 2){ BallWorld gw = (BallWorld) getWorld(); gw.dispRed(); //NullPointerException occurs here } else{ //future code } } }
danpost danpost

2013/9/3

#
Looking at the code, I cannot tell where the nullPointerException is coming from. Try changing the line
// in RandomPicker  class 'eject' method
gw.dispRed();
// to
getWorld().addObject(new RedBall(), 366, 380);
If you get the same error, put this line before it
System.out.println("In world is: "+(getWorld() != null));
and post back with the results.
mukkainaresh mukkainaresh

2013/9/3

#
danpost wrote...
Looking at the code, I cannot tell where the nullPointerException is coming from. Try changing the line
// in RandomPicker  class 'eject' method
gw.dispRed();
// to
getWorld().addObject(new RedBall(), 366, 380);
If you get the same error, put this line before it
System.out.println("In world is: "+(getWorld() != null));
and post back with the results.
I got the same error. The output is
In world is: false
danpost danpost

2013/9/3

#
That means your RandomPicker object has been removed from the world. I see that your world constructor calls the 'prepare' method that adds an object of type RandomPicker into the world; maybe your Picker class has an act method that has something to do with it. Another possible is where the 'pop' method is called from; you may need to review the code there.
mukkainaresh mukkainaresh

2013/9/4

#
Hi Dan, In order to call a method in RandomPicker class, I am creating another object in Inspector class. I am not using the one from populate(). class Inspector(){ RandomPicker randomPicker = new RandomPicker(); randomPicker.pop(); } Thats how it is called.
mukkainaresh mukkainaresh

2013/9/4

#
Also, the act() in picker class is empty.
mukkainaresh mukkainaresh

2013/9/4

#
public class Inspector extends Alien { public void pop( String quarter ) { if ( quarter != "Null" ){ RandomPicker randomPicker = new RandomPicker(); randomPicker.pop(); } } } This is the exact code.
danpost danpost

2013/9/4

#
The problem is with these two lines:
RandomPicker randomPicker = new RandomPicker();
randomPicker.pop();
Line 1 creates a new RandomPicker object which is not yet added into the world. Line 2 calls the 'pop' method, which requires the RandomPicker object to be in a world because it calls the 'eject' method which uses the 'getWorld()' method which returns 'null' if the RandomPicker object is not in a world. The error occurs on the following line because you are trying to call the method 'dispRed' on 'null' (a non-existent world).
mukkainaresh mukkainaresh

2013/9/4

#
how can I refer to the randomPicker in the world?
There are more replies on the next page.
1
2