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

2017/9/11

nullPointerExeption

Nosson1459 Nosson1459

2017/9/11

#
could someone tell me why this is throwing a null pointer I can't figure it out
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Object here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Object extends Actor
{
    private int x;
    private int y;
    public Object()
    {
        
    }
    
    protected void addedToWorld(World world)
    {
    }
    
    /**
     * Act - do whatever the Object wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(),getY());       
    }   
    
    @Override
    public int getX()
    {
        ScrollActor actor=(ScrollActor)getOneIntersectingObject(ScrollActor.class);
        return Math.abs(actor.calcX()-super.getX());
    }
    
    @Override
    public int getY()
    {
        ScrollActor actor=(ScrollActor)getOneIntersectingObject(ScrollActor.class);
        return Math.abs(actor.calcY()-super.getY());
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ScrollActor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ScrollActor extends Actor
{
    private int speed=10;
    public ScrollActor()
    {
        setImage("Scroll.jpg");
    }

    /**
     * Act - do whatever the ScrollActor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        scrolling();
        getWorld().showText(""+getX(),250,235);
        getWorld().showText(""+getY(),250,265);
    }    
    
    /**
     * Tell the scrollActor where and when to move.
     */
    private void scrolling()
    {
        if(Greenfoot.isKeyDown("up")&&getY()<1245)setLocation(getX(),getY()+speed);
        if(Greenfoot.isKeyDown("down")&&getY()>-745)setLocation(getX(),getY()-speed);
        if(Greenfoot.isKeyDown("right")&&getX()>-745)setLocation(getX()-speed,getY());
        if(Greenfoot.isKeyDown("left")&&getX()<1245)setLocation(getX()+speed,getY());
    }
    
    /**
     * calculate the coord of the new 0 point on the x axis.
     */
    public int calcX()
    {
        return getX()-(getImage().getWidth()/2);
    }
    
    /**
     * calculate the coord of the new 0 point on the y axis.
     */
    public int calcY()
    {
        return getY()-(getImage().getHeight()/2);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Olam here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Olam extends World
{
    private ScrollActor scroller=new ScrollActor();
    /**
     * Constructor for objects of class Olam.
     * 
     */
    public Olam()
    {    
        super(500, 500, 1,false); 
        addObject(scroller,250,250);
        Greenfoot.start();
    }
}
this is the terminal window
java.lang.NullPointerException
	at Object.getX(Object.java:39)
	at Object.act(Object.java:29)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2017/9/11

#
Your 'getX' and 'getY' methods in the Object class are trying to return a value from an actor that may or may not be present. If one is not present, you cannot possibly use 'calcX' on 'actor'. Check to make sure 'actor' is not null before calling 'calcX' on it. However, I do not know what value you would then return. Maybe return an Integer object instead of an int value so 'null' can be returned and the calling method can deal with that possibility. As a sidenote: I would refrain from using the name 'Object' for a class as ALL classes are already subclasses of an 'Object' class.
You need to login to post a reply.