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/17

#
java.lang.NullPointerException at Barrel.createNewBarrel1(Barrel.java:102) at Barrel.getTwoPossiblities(Barrel.java:87) at Barrel.createInIntervalls(Barrel.java:78) at Barrel.act(Barrel.java:35) This is the act Method, where the Error is: public void act() { move(-2); block(); clean(); } public boolean atWorldEdge() { if(stickman.getX()>getX()+99) { return true; } else { return false; } } public void clean() { if(getWorld() != null && atWorldEdge()) { getTwoPossiblities(); getWorld().removeObject(this); } } public void block() { if(getWorld() != null && canSee(Stickman.class)) { Greenfoot.stop(); } } public void getTwoPossiblities() { if(getWorld() != null) { Greenfoot.getRandomNumber(1); if(Greenfoot.getRandomNumber(1)==0) { createNewBarrel1(); } if(Greenfoot.getRandomNumber(1)==1) { createNewBarrel2(); } } } private void createNewBarrel1() { Stickman stickman = new Stickman(); Barrel newBarrel; newBarrel = new Barrel(stickman); getWorld().addObject(newBarrel, 1000, 280); } private void createNewBarrel2() { Stickman stickman = new Stickman(); Barrel newBarrel; newBarrel = new Barrel(stickman); getWorld().addObject(newBarrel, 1000, 220); }
lordhershey lordhershey

2014/3/17

#
I see a call to a a method called createInIntervalls, is it possible that you have an actor who is in the world call a method on a new instance that is not? Can you put the entire barrel class in the post (inside of the code tags, press the Code link next to the quote link below the text area) Also this here Greenfoot.getRandomNumber(1) will always be 0, you need to do Greenfoot.getRandomNumber(2) for a 0..1 range.
Drizzt Drizzt

2014/3/17

#
no getRandomNumber(1) is from 0 two 1 I already tested it, that works, but there is an Error atWorldEdge(Barrel.java:44) at Barrel.clean(Barrel.java:57) at Barrel.act(Barrel.java:35) I changed it a bit createInIntervalls is not there anymore
Zamoht Zamoht

2014/3/17

#
If getTwoPossiblities is called before the object is added to a world, getWorld() will return null and the error will occur.
Drizzt Drizzt

2014/3/17

#
ahhh ok, thats true, now the Error is cleared, but now there where no new Barrels added to the world :(
danpost danpost

2014/3/17

#
Please show the entire Barrel class. Insert the code in your post by way of the 'code' link below the 'Post a reply' box.
Drizzt Drizzt

2014/3/17

#
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
{
    public Stickman stickman;
    
    
    
   
    
    public Barrel(Stickman playerPosition)
    {
        stickman = playerPosition;

    }

    
    /**
     * Act - do whatever the Barrel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
       
    public void act() 
    {
        move(-2);
        block();
        clean();
    
        
        
        
    }

    public boolean atWorldEdge()
    { 
        if(stickman.getX()>getX()+99) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void clean()
    {

        if(getWorld() != null && atWorldEdge())
        {
            getWorld().removeObject(this);
            getTwoPossiblities();

        }
        
    }
    public void block()
    {
        
        if(getWorld() != null && canSee(Stickman.class))
        {
            Greenfoot.stop();
            
            
            
        }
    }
    
    
    public void getTwoPossiblities()
    {  
    if(getWorld() != null)
    {
        Greenfoot.getRandomNumber(1);
        if(Greenfoot.getRandomNumber(1)==0)
        {
            createNewBarrel1();
        }  
        if(Greenfoot.getRandomNumber(1)==1)
        {
            createNewBarrel2();
        }
    }
    }
    
    
    private void createNewBarrel1()
    {
       Stickman stickman = new Stickman();
        Barrel newBarrel;
        newBarrel = new Barrel(stickman);
        
       getWorld().addObject(newBarrel, 1000, 280);
        
    }
    private void createNewBarrel2()
    {
        Stickman stickman = new Stickman();
        Barrel newBarrel;
        newBarrel = new Barrel(stickman);
        
        getWorld().addObject(newBarrel, 1000, 220);
    }
   
     

    
    
        
}
danpost danpost

2014/3/17

#
Is each Barrel object supposed to have a separate Stickman object? or are all Barrel objects supposed to be moving away from the same Stickman object? I think the main problem is that you are creating new Stickman objects at lines 97 and 106 which are never placed into the world. So, 'act' calls 'clean' on these new Barrel objects that these new stickmen are assigned to and 'stickman.getX()' causes an 'IllegalStateException'.
Drizzt Drizzt

2014/3/17

#
ah ok, that could be the problem. The hole World is moving with all actors in it and the barrels move against it. And yes its only one Stickman. I deleted the new Stickman stuff, but never the less, there where no new barrels added
Zamoht Zamoht

2014/3/17

#
Add getTwoPossiblities() to the actors addedToWorld(World world) method. This method gets called when the object is added to the world which is what i bet you are looking for.
Drizzt Drizzt

2014/3/17

#
so there is no real error anymore, but no barrels where added. They were already deleted at WorldEdge, but there is no new one, I really don't know why :(
danpost danpost

2014/3/17

#
Let me ask you this: why do the Barrel objects need to know where stickman is? and what type of movement does the stickman have? if it does not move, what is its location? What I see in your 'atWorldEdge' method is some type of attempt to limit the barrel to within 100 cells of the stickman.
Drizzt Drizzt

2014/3/18

#
The whole world is moving and I can't define the World Edge, so I have to make it depend on the Stickman getX(), so I need the condition between Barrel and Stickman. And the World Edge is already working like this, because the barrels where destroyed atEdge, but no Barrels where added.
Drizzt Drizzt

2014/3/18

#
protected  void 	addedToWorld(World world) 
      {
    
         getTwoPossibilities();
        
    }
This isn't working Zamoht, I get an java.lang.StackOverflowError
Zamoht Zamoht

2014/3/18

#
Try making it a public void
There are more replies on the next page.
1
2