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

2015/9/21

NullPointerException

styxz99 styxz99

2015/9/21

#
So I am trying to make my actor run through a maze until all of the flowers have been collected. I turned the number of flowers already in the world into an integer using this line of code. int f = getWorld().getObjects(Flower.class).size(); But whenever I try and add my actor to the world by right-clicking it, java gives me a NullPointerException error at the code above. Any help is appreciated, thank you!
styxz99 styxz99

2015/9/21

#
I am actually having the same NullPointerException error Doaky is here: http://www.greenfoot.org/topics/54658/15. How exactly did he call the 'super' Actor?
danpost danpost

2015/9/22

#
styxz99 wrote...
I am actually having the same NullPointerException error Doaky is here: http://www.greenfoot.org/topics/54658/15. How exactly did he call the 'super' Actor?
The question is not "How", but "Where". It, 'getWorld().getObjects', cannot be called until the actor is in the world. That means, no code that is executed before the actor is in the world can use it (this includes, but may not be limited to, field declarations and the class constructor). As a side-note, 'getWorld' will not in itself error as it will return 'null', if the actor is not in a world, or the world the actor is in. It is calling a method, like 'getObjects', on that returned value when its value is 'null' that causes the error (you cannot get a list of objects from a non-existent world)..
8cm8 8cm8

2015/9/22

#
Looking at the Doaky's code, I thought the actor was called before "int worldWidth = world.getWidth();". He calls it in with "super()", right? I'm having a similar issue here
danpost danpost

2015/9/22

#
8cm8 wrote...
Looking at the Doaky's code, I thought the actor was called before "int worldWidth = world.getWidth();". He calls it in with "super()", right? I'm having a similar issue here
You need to show the code of the class so that we can tell you what is wrong with it. Please show the entire class (do not leave out the imports or anything else).
styxz99 styxz99

2015/9/22

#
Thank you for the help! I'm not sure how I would go about fixing this though. The only way I have been putting the actor into the world is manually. How would I go about making sure the actor is in the world first before calling the getWorld().getObjects() if I'm not using a statement like this.add()? Sorry for asking so many questions I'm pretty new to coding.
styxz99 styxz99

2015/9/22

#
We should only be coding in the actor's subclass so the this.add(actor); statement doesn't seem to work. I'm pretty confused at how to add the actor to the world using code. Here's a portion of my code if that helps, sorry for the double post: public MazeRunner() { super(1, 1, 10); } int worldWidth = getWorld().getWidth(); int worldHeight = getWorld().getHeight();
danpost danpost

2015/9/22

#
The best way to learn is to add the actors manually and then use the 'Save the World' feature to write the code for you. Then look at the code in your World subclass and compare it to what actions you performed before saving the world. You can use that to learn how it is coded.
You need to login to post a reply.