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

2013/3/12

Error with a scrolling world.

steved steved

2013/3/12

#
Okay so I am making a platform game and I have the ground move instead of the player when he gets within a certain didtance from the edge. But when I try to make the ground move according to some variable in my world class I get this error:
java.lang.NullPointerException
	at Ground.act(Ground.java:14)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
All I know is that there is something wrong with line 14 of the Ground.class. Not certain exactly what a null pointer exception is but it is refering to a line of code telling it to set one of its variables to one of the worlds. I get the error when I press act.
steved steved

2013/3/12

#
Here is the Ground.class:
public class Ground extends Actor
{
    CubeWorld cubeWorld = (CubeWorld) getWorld();
    public void act()
    {
        boolean isScrolling = cubeWorld.isScrolling;   
        if(isScrolling)
        {
            int scrollingSpeedRL = cubeWorld.scrollingSpeedRL;
            scrollLR(scrollingSpeedRL);
        }
    }

    public void scrollLR(int speed)
    {
        setLocation(getX() - speed, getY());
    }
Line 14 id line 6 because I didnt copy comments or the import at the top.
bourne bourne

2013/3/12

#
Post line 14 from Ground's code. Null pointer exception occurs when an Object is null and has been attempted to be dereferenced.
steved steved

2013/3/12

#
It does its just line 6 here.
bourne bourne

2013/3/12

#
It looks like CubeWorld will always be null. Since you call the following when the Actor is initiated (?), at which point in time the Actor cannot be already in the World. CubeWorld cubeWorld = (CubeWorld) getWorld();
bourne bourne

2013/3/12

#
Do something like this:
 
    public void act()  
    {  
CubeWorld cubeWorld = (CubeWorld) getWorld(); 
        boolean isScrolling = cubeWorld.isScrolling;     
        if(isScrolling)  
        {  
            int scrollingSpeedRL = cubeWorld.scrollingSpeedRL;  
            scrollLR(scrollingSpeedRL);  
        }  
    }  
steved steved

2013/3/12

#
Thanks I fixed it now. you were right, all I needed to do was move the CubeWorld cubeWorld = (CubeWorld) getWorld(); into the act method. Testing to see if it actually scrolls now.
steved steved

2013/3/12

#
Well it scrolls...... It just doesnt do what I want to. I think i see the problem and I'm pretty sure I can fix it.
steved steved

2013/3/12

#
Okay got it fixed. Thanks again for all of the help.
bourne bourne

2013/3/12

#
No problem.
You need to login to post a reply.