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

2014/3/17

Problem with java.lang.NullPointerException

1
2
Drizzt Drizzt

2014/3/18

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Iterator;
/**
 * Write a description of class Barrel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Barrel extends Animal
{
    /**
     * Act - do whatever the Barrel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Barrel(Stickman playerposition)
    {
        stickman = playerposition;
    }
    Stickman stickman;
    public void act() 
    {
        move(-2);
        block();
        clean();      
    }
    public boolean atWorldEdge()
    { 
        if(stickman.getX()>getX()+99 && getWorld() != null) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void clean()
    {
        if(getWorld() != null && atWorldEdge())
        {
            getWorld().removeObject(this);
            getTwoPossibilities();
        }
    }
    public void block()
    {
        
        if(getWorld() != null && canSee(Stickman.class))
        {
            Greenfoot.stop();
        }
    }
    public void getTwoPossibilities()
    {  
    if(getWorld() != null)
    {
        Greenfoot.getRandomNumber(1);
        if(Greenfoot.getRandomNumber(1)==0)
        {
            createNewBarrel1();
        }  
        if(Greenfoot.getRandomNumber(1)==1)
        {
            createNewBarrel2();
        }
    }
    }
    private void createNewBarrel1()
    {
        Barrel newBarrel;
        newBarrel = new Barrel(stickman);
       getWorld().addObject(newBarrel, 1000, 280);
    }
    private void createNewBarrel2()
    {
        Barrel newBarrel;
        newBarrel = new Barrel(stickman);
        getWorld().addObject(newBarrel, 1000, 220);
    }  
}
Why is he doing the first thing at WorldEdge, but not the second? I think the Error must be there, because when I transfer the getTwoPossibilities Method into the act Method, something happens
Drizzt Drizzt

2014/3/18

#
public void addedToWorld(World world) { getTwoPossibilities(); } This also gets an java.lang.StackOverflowError
Zamoht Zamoht

2014/3/18

#
I believe the problem is in the clean method. You first delete the object (the world reference is removed from your actor) and then you test if world != null. So my suggestion is to change clean to the following:
    public void clean()  
    {  
        if(getWorld() != null && atWorldEdge())  
        {  
            getTwoPossibilities(); 
            getWorld().removeObject(this);  
        }  
    }  
danpost danpost

2014/3/18

#
The stack overflow problem is due to the fact that every time you 'create a Barrel object and add it to the world', it then creates a new Barrel object and adds it to the world, and so forth; and so forth. This is because you are calling 'getTwoPossibilities' from the 'addedToWorld' method.
danpost danpost

2014/3/18

#
If we remove the possibilities that 'getWorld' will return null (it seems you have this in almost every method) and simplify a little, we can get this:
import greenfoot.*;

public class Barrel extends Actor
{
    Stickman stickman;

    public Barrel(Stickman playerposition)
    {
        stickman = playerposition;
    }

    public void act() 
    {
        move(-2);
        if (isTouching(Stickman.class)) Greenfoot.stop(); else clean();
    }

    public void clean()
    {
        if (stickman.getX() > getX()+99)
           setLocation(1000, 220+Greenfoot.getRandomNumber(2)*60);
    }
}
Now, what happened to all that code you had? (and the Animal class can be removed--unless you have another class extending it). I never did much like the Animal class. It had three methods -- two of them are basically one-liners; one (the 'canSee' method is now part of the Actor class as 'isTouching'); the 'atWorldEdge' method is too specific for most scenarios; and the 'eat' method requires that you already know an object of said type is intersecting to begin with, which means you are calling the intersection detection method twice.
Drizzt Drizzt

2014/3/18

#
Ah yes, thanks men, thats a really nice solution! I have not much skill in Greenfoot, so there was so much code. Thanks for helping me. :)
You need to login to post a reply.
1
2