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

2015/5/7

Null Pointer Exception error upon compiling

bennordin21 bennordin21

2015/5/7

#
Why is this happening? java.lang.NullPointerException at Cpu.<init>(Cpu.java:12) at CpuWorld.populate(CpuWorld.java:40) at CpuWorld.<init>(CpuWorld.java:21) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at greenfoot.core.Simulation.newInstance(Simulation.java:596) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:437) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:483) at greenfoot.core.Simulation.maybePause(Simulation.java:296) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cpu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cpu extends Mover
{

    Ball ball = (Ball)(getWorld().getObjects(Ball.class)).get(0);
    
    Arrow arrow = (Arrow)(getWorld().getObjects(Arrow.class)).get(0);

    public void act()
    {

        reset();
        
        if(arrow.getRotation() == -90)
        {
            setEasy();
        }
        else if(arrow.getRotation() == 0)
        {
            setMedium();
        }
        else if(arrow.getRotation() == 90)
        {
            setHard();
        }
    }

    public void setEasy()
    {
            
        if (ball.getY() > getY() && ball.getX() > 300)
        {
            walkDown(1);

        }
        if (ball.getY() < getY() && ball.getX() > 300)
        {
            walkUp(1);

        }
    }

    public void setMedium()
    {
            
        if (ball.getY() > getY() && ball.getX() > 300)
        {
            walkDown(2);

        }
        if (ball.getY() < getY() && ball.getX() > 300)
        {
            walkUp(2);

        }

    }

    public void setHard()
    {
           
        if (ball.getY() > getY() && ball.getX() > 300)
        {
            walkDown(5);

        }
        if (ball.getY() < getY() && ball.getX() > 300)
        {
            walkUp(5);

        }

    }

    
  
    public void reset()
    {
        

        if(ball.getX() <= 5 || ball.getX() >= 995)
        {
            setLocation(955, 300);
        }
    }

    
    
    
    
    
    
    
    
    
    
}
azazeel azazeel

2015/5/7

#
what did you expect from this code : Ball ball = (Ball)(getWorld().getObjects(Ball.class)).get(0);
danpost danpost

2015/5/7

#
The 'ball' and 'arrow' fields are given to the new object and assigned their values during the creation of the Cpu object. This happens before the object is added into any world. Therefore, using 'getWorld' in their value assignments must return a 'null' value. You cannot assign those values using 'getWorld' until the object is added into the world.
bennordin21 bennordin21

2015/5/7

#
danpost wrote...
The 'ball' and 'arrow' fields are given to the new object and assigned their values during the creation of the Cpu object. This happens before the object is added into any world. Therefore, using 'getWorld' in their value assignments must return a 'null' value. You cannot assign those values using 'getWorld' until the object is added into the world.
Thank you so much!
You need to login to post a reply.