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

2013/8/28

Help Please

1
2
3
4
mattjames mattjames

2013/9/2

#
Used code as above, but when I ran the game it came up with a null pointer exception on the code below
world worldWorld = (world) getWorld();  
    Counter3 counter3 = worldWorld.getCounter3();  
    int choice = counter3.getCount3);
Here is the code from the Counter 3 class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter3  extends Actor
{
    private int totalCount = 1;

    public Counter3()
    {
        setImage(new GreenfootImage("", 1, Color.WHITE, Color.WHITE));
    }
    public int getCount()  
    {  
     return totalCount;  
    } 
    /**
     * Increase the total amount displayed on the counter, by a given amount.
     */
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
    }
    public void setCount(int amount)
    {
        totalCount = amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
    }
}
SPower SPower

2013/9/2

#
You miss a ( and have a 3 too much
// you have this:
int choise = counter3.getCount3);
// you should have:
int choise = counter3.getCount();
mattjames mattjames

2013/9/2

#
Still a problem with this line Counter3 counter3 = worldWorld.getCounter3();
SPower SPower

2013/9/2

#
What does the error say?
danpost danpost

2013/9/2

#
The object whose act method is running was probably removed from the world before this line of code was executed. 'getWorld()' is probably returning 'null' and therefore cannot use 'getCounter3()' on 'null'.
mattjames mattjames

2013/9/2

#
It gives a java.lang.NullPointerException and the counter3 class is never removed
danpost danpost

2013/9/2

#
You need to show the code for the class that executes the code the error occurs in.
mattjames mattjames

2013/9/2

#
The counter class or the one where I am using the counter?
danpost danpost

2013/9/2

#
The one where you have this code in:
world worldWorld = (world) getWorld();  
    Counter3 counter3 = worldWorld.getCounter3();  
    int choice = counter3.getCount3);
mattjames mattjames

2013/9/2

#
You won't want/need the whole thing but here is the necessary parts
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class robottrackslarge here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bus extends Actor
{
world worldWorld = (world) getWorld();  
    Counter3 counter3 = worldWorld.getCounter3();  
    int choice = counter3.getCount();    
private Counter2 theCounter2;
        private Counter theCounter;
    private int maximumcarrying=0; 
    private int score=0;
    private int scorelevelone=150;//this sets the score
    private int scoreleveltwo=150;
    private boolean win=false;
    private int targetscore; //may need to remove this
    private int level=1;
    private int passengersleft=4;
    private int passengerscarrying=0;
    //need to use this to set maximum bay size!!!!!!!!!!!
    


    
    public void act() 
    {

        scoretester();// Add your action code here.
    }  
    //maybe try using a boolean to see whether the game has been won


    public void scoretester(){ //this part checks to see that the game hasnt been won yet
        world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        
        if (passengersleft==0&&counter.getCount()>0){
            win=true;
        }
        else if(counter.getCount()<1&& 
        passengersleft>0){
            //display failure code and send to main menu.
            
        }
        else if(counter.getCount()>0&&
        passengersleft>0){
            canmove();
            
        }
        
    }

    
    public void canmove(){
        int locationy =getY();
        int testedlocationy;
        testedlocationy=locationy+1;

        if(testedlocationy>9){

            movementnotdown();

        }
        else{
            movement();
        }
    }

    public void movement()
    {
        if(Greenfoot.isKeyDown("right"))
        { 

                move(1);
                Greenfoot.delay(10);
                floortesterIce();

            
        }
            else if(Greenfoot.isKeyDown("left"))
            {
                move(-1);
                Greenfoot.delay(10);
                floortesterIce();

            }
            else if(Greenfoot.isKeyDown("up"))
            {
                setLocation(getX(),getY()-1);
                Greenfoot.delay(10);
                floortesterIce();

            }
            else if(Greenfoot.isKeyDown("down"))
            {
                setLocation(getX(),getY()+1);
                Greenfoot.delay(10);
                floortesterIce();
            }
        }
    

    public void movementnotdown()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            move(1);
            Greenfoot.delay(10);
            floortesterIce();

        }
        else if(Greenfoot.isKeyDown("left"))
        {
            move(-1);
            Greenfoot.delay(10);
            floortesterIce();
        }
        else if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(),getY()-1);
            Greenfoot.delay(10);
            floortesterIce();
        }

    }

    public void floortesterIce()
    {
        Actor Ice;    
        Ice=getOneObjectAtOffset(0,0,Ice.class);
        if(Ice != null) {
            //this needs to go

            scoringmovementice();

        }
        else{
            floortestergrass();
        }
    }

    public void floortestergrass()
    {
        Actor grass;    
        grass=getOneObjectAtOffset(0,0,grass.class);

        if(grass != null) {
            
            scoringmovementgrass();

        }
        else{
            floortesterrock();
        }
    }

    public void floortesterrock()
    {
        Actor rock;    
        rock=getOneObjectAtOffset(0,0,rock.class);

        if(rock != null) {
            
            scoringmovementrock();

        }
        else{
            canmove();
        }
    }
    

    public void scoringmovementgrass(){
        //insert code here to check modification and then reduce accordingly
        if (choice==1){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==2){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==3){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==4){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==5){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==6){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==7){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==8){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==9){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        

    }

    public void scoringmovementrock(){
                //insert code here to check modification and then reduce accordingly   
        if (choice==1){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==2){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==3){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==4){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==5){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==6){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==7){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==8){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==9){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }

    }

    public void scoringmovementice(){
                //insert code here to check modification and then reduce accordingly  
        if (choice==1){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==2){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==3){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==4){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==5){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==6){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==7){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==8){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }
        if (choice==9){
            world worldWorld = (world) getWorld();
        Counter counter = worldWorld.getCounter();
        counter.bumpCount(-5);
        passengerpickuptester();
            
            
        }

    }
    public void passengerpickuptester(){
        //insert code using counter 3 to test how many people can carry and save that as variable maximumcarrying
        //this bit will test whether the robot can pickup a person
        //if true go to passengerpickup
        //if not go to at basecamp
        
        
        
        
    }

    public void passengerpickup() {
        Actor person;    
        person=getOneObjectAtOffset(0,0,person.class);

        if(person != null) {
            World world;

            world=getWorld();
            world.removeObject(person);
            passengerscarrying=passengerscarrying++;
            passengersleft=passengersleft--;

            scoringpassengers();
        }
        else{
            scoretester();

        }
    }
    public void scoringpassengers(){

        world worldWorld = (world) getWorld();
        Counter2 counter2 = worldWorld.getCounter2();
        counter2.bumpCount2(1);
        
    } 
    public void atbasecamp(){
        int locationy =getY();
        int locationx =getX();

        int basecamptestlocationX;
        basecamptestlocationX=locationx;

        int basecamptestlocationy;
        basecamptestlocationy=locationy;

        if(basecamptestlocationX==6&&basecamptestlocationy==3){
            basecampunload();

        }
        else{
            canmove();

        }
    }

    public void basecampunload(){
        if(passengerscarrying>0){
            world worldWorld = (world) getWorld();
        Counter2 counter2 = worldWorld.getCounter2();
        counter2.bumpCount2(-1); //need to sort this
            passengersleft=passengersleft-passengerscarrying;
            passengerscarrying=0;
            scoretester();

        }
        else{
            canmove();

        }

    }

    
}
Sorry there is so much....
danpost danpost

2013/9/2

#
Line 11 of the bus class will set 'worldWorld' to 'null' as the bus actor is not in the world during construction (executing 'new bus()' creates the object by running the constructor; executing 'addObject' adds the object into the world and only then will 'getWorld' return the world it is in).
mattjames mattjames

2013/9/2

#
Ah I get that , but where should I put it?
danpost danpost

2013/9/2

#
You can just declare the fields in lines 11 through 13 without assigning values to them and use the 'addedToWorld(World) method to set the values:
public void addedToWorld(World w)
{
    worldWorld = w;
    counter3 = w.getCounter3();
    choice = counter3.getCount();
}
This method, which is part of the Actor class, will be called automatically when the bus actor is added into a world (you do not programmatically call it).
davmac davmac

2013/9/2

#
You will still need the typecast for assigning to worldWorld on line 3 of danpost's code:
    worldWorld = (world) w;  
Again, I strongly encourage you to rename the 'world' class.
danpost danpost

2013/9/2

#
davmac wrote...
Again, I strongly encourage you to rename the 'world' class.
Yeah, that does get confusing.
There are more replies on the next page.
1
2
3
4