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

2014/7/15

java.lang.NullPointerException: The given world cannot be null.?!?!?!?!

miku622 miku622

2014/7/15

#
I'm trying to make it so after you click on an actor on the home page, it will direct you to another page. However, I keep getting an issue. PLEASE HELP??? java.lang.NullPointerException: The given world cannot be null. at greenfoot.Greenfoot.setWorld(Greenfoot.java:70) at Ladybug2.act(Ladybug2.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) My actor [Disallowed URL] code looks like this:
miku622 miku622

2014/7/15

#
Sorry I meant like this: I am new to greenfoot import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Ladybug2 extends Actor { Actor Ladybug2; World TitlePage; public void mouseClicked() { if (Ladybug2 !=null) { World world; world=getWorld(); } } public void act() { if (Greenfoot.mouseClicked(Ladybug2)){ Greenfoot.setWorld(TitlePage); // Add your action code here. } } }
danpost danpost

2014/7/15

#
Your 'TitlePage' field is never assign a value (a world to refer to). Nor is your 'Ladybug2' field. The thing is -- these fields are not necessary. Nor is the 'mouseClicked' method (the method before the 'act' method). All you need is the 'act' method; but, it needs a little work. First 'Greenfoot.mouseClicked' required an object instance (or null) for the parameter, not a class name. And 'Greenfoot.setWorld' requires a World instance (never null) for the parameter, not a class name (again). You should avoid using a name that are already assigned to a class as a field name. That is 'Actor ladybug2' would be much better than 'Actor Ladybug2' (or, 'Ladybug2 Ladybug2'). Anyway, I have a feeling that you added the 'Ladybug2' field to refer to your Ladybug2 instance that was added into your world. This is no needed because the act method is executed for each instance of the class that is in the active world and the 'this' keyword' will refer to the instance of that class that the method is being executed for (please refer to this page of the java tutorials for more info on the 'this' keyword'). Then, for the world instance in the 'setWorld' method, create a new TitlePage instance to place into the call.
miku622 miku622

2014/7/15

#
Ok i get it thanks!
You need to login to post a reply.