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

2016/3/23

Null pointer exception

ryvetra ryvetra

2016/3/23

#
i want this brick to pop up on upper edge, every time this brick remove in bottom edge ( like animation )
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Brick here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Brick extends Actor
{
     private static final int ACCEL = 5;
    private int atas = 15;
    /**
     * Act - do whatever the Brick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
            setLocation(getX(), getY()+ACCEL);
           // atas = atas + ACCEL;
           checkout();
    }
    public void checkout()
    {
        if (getWorld() != null && (this.atWorldEdge()) ) {
            
            getWorld().removeObject(this);
            getWorld().addObject(new Brick(), 13, 20);
            getWorld().addObject(new Brick(), 227, 20);
            return;
    
        }
    }
    public boolean atWorldEdge()
    {
        if(getWorld() != null && (getX() < 5 || getX() > getWorld().getWidth() - 5))
            return true;            
        if(getWorld() != null && (getY() < 5 || getY() > getWorld().getHeight() - 5))
            return true;
        else
            return false;
    }
    }    
java.lang.NullPointerException at Brick.checkout(Brick.java:29) at Brick.act(Brick.java:22) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) and this is the world class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private static final int GAP = 20;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(240, 450, 1); 
        int y = 20;
        while( y < 450)
        {
            addObject(new Brick(),227,y);
            y = y + 30 + GAP;
        }
        
        int y2 = 20;
        while( y2 < 450)
        {
            addObject(new Brick(),13,y2);
            y2 = y2 + 30 + GAP;
        }
        addObject(new Car(),121 , 405);
    }
    
}
Super_Hippo Super_Hippo

2016/3/23

#
Swap lines 28 and 30 of the Brick class.
ryvetra ryvetra

2016/3/23

#
thank you , it works :) i have another question i want this mobil ( english = car) to pop up between 128 - 225(( not edge ) horizontal.
addObject(new Mobil(), Greenfoot.getRandomNumber(getWidth())+128, 15);
Super_Hippo Super_Hippo

2016/3/23

#
For the x-value, it will be:
Greenfoot.getRandomNumber(98)+128
ryvetra ryvetra

2016/3/23

#
thanks .. it work again :D but after it works , the brick animation somehow broke , like spawn faster/slower , spawn in x = 120 , not 150. is it the greenfoot ? or ? please ignore the class/method named, i have'nt change it yet thanks World class :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private static final int GAP = 40;
    private int spawnCounterRock = 0;
    private int spawnCounterRock2 = 0;
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(300, 450, 1); 
        int y = 7;
        while( y < 443)
        {
            addObject(new Brick(),150,y);
            y = y + 30 + GAP;
        }
        
        addObject(new Car(),160 , 405);
        setPaintOrder(Car.class , Mobil.class);
    }
    public void act()
    {
        spawnRock();
    }
    private void spawnRock()
    {
        if (spawnCounterRock > 50) { 
            addObject(new Mobil(), Greenfoot.getRandomNumber(98)+160, 15);
            spawnCounterRock = 0; 
        }        
        spawnCounterRock++;
        if (spawnCounterRock2 > 50) { 
            addObject(new Mobil2(), Greenfoot.getRandomNumber(getWidth())-160, 15);
            spawnCounterRock2 = 0; 
        }        
        spawnCounterRock2++;
    }
}
Brick Class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Brick here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Brick extends Actor
{
     private static final int ACCEL = 4;
    private int atas = 15;
    /**
     * Act - do whatever the Brick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
            setLocation(getX(), getY()+ACCEL);
           // atas = atas + ACCEL;
           checkout();
    }
    public void checkout()
    {
        if (getWorld() != null && (this.atWorldEdge()) ) {
            
            getWorld().addObject(new Brick(), 150, 7);            
            //getWorld().addObject(new Brick(), 13, 5);
            getWorld().removeObject(this);
            return;
    
        }
    }
    public boolean atWorldEdge()
    {
        if(getWorld() != null && (getY() < 10 || getY() > getWorld().getHeight() - 10))
            return true;
        else
            return false;
    }
    }   
Mobil Class :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Mobil here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mobil extends Actor
{
    /**
     * Act - do whatever the Mobil wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       setLocation(getX(), getY()+2);
       checkout();
    }    
    public void checkout()
    {
        if (getWorld() != null && (this.atWorldEdge()) ) {
            
            getWorld().addObject(new Brick(), 120, 5);            
            //getWorld().addObject(new Brick(), 13, 5);
            getWorld().removeObject(this);
            return;
    
        }
    }
    public boolean atWorldEdge()
    {
        if(getWorld() != null && (getX() < 5 || getX() > getWorld().getWidth() - 5))
            return true;            
        if(getWorld() != null && (getY() < 5 || getY() > getWorld().getHeight() - 5))
            return true;
        else
            return false;
    }
}
danpost danpost

2016/3/23

#
It appears that line 24 of your Mobil class has bricks spawning at x=120.
ryvetra ryvetra

2016/3/23

#
my mistake .. thanks :)
You need to login to post a reply.