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

2020/11/22

getting Nullpointerexception, dont know how to solve it.

1
2
Hellotheremyfriend Hellotheremyfriend

2020/11/22

#
/**
 * Write a description of class Leukozyte here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Leukozyte extends Actor
{
    
    World w =getWorld();
    public void act() 
    {
        
        
        move(5);
        rechts();
        links();
        fire();
        
    }  
    public void fire()
    {
        if(Greenfoot.mousePressed(null))
        {
            Proteingeschoss p = new Proteingeschoss();
            w.addObject(p, getX(), getY());
            p.turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
        }
    }
    /**
     * Wenn der Key "d" dedrückt wird, dann dreht sich die Leukozyte im Uhrzeigersinn.
     */
    public void rechts()
    {
        if(Greenfoot.isKeyDown("d"))
        {
            turn(5);
        }
    }
    /**
     * Wenn der Key "a" gedrückt wird, dann dreht sich die Leukozyte gegen den Uhrzeigersinn.
     */
    public void links()
    {
        if(Greenfoot.isKeyDown("a"))
        {
            turn(-5);
           
        }
    }
   
           
}

public class Proteingeschoss extends Actor
{
     /**
     * Act - do whatever the Protonengeschoss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int speed = 6;
    public void act() 
    {
        
        
        turnToMouse();
        move(speed);
        destroyEnemies();
        atEdge();

    }    
    public void turnToMouse()
    {
        turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
    }
    
     /**
     * Durch die Methode "destroyEnemies()" werden Feinde bzw. feindliche Organismen zerstört
     */
    public void destroyEnemies()
    {
        if(isTouching(Mikrobe.class))
        {
            removeTouching(Mikrobe.class);
        }
        if(isTouching(Fungi.class))
        {
            removeTouching(Fungi.class);
        }
        if(isTouching(Bakterium.class))
        {
            removeTouching(Bakterium.class);
        }
            
    }
    public void atEdge()
    {
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
    
    
}
danpost danpost

2020/11/22

#
World w is null. It is assigned its value when the actor is created -- before it is added into any world. Remove line 10 and replace "w" on line 26 with "getWorld()".
Hellotheremyfriend Hellotheremyfriend

2020/11/22

#
Thanks, but now after some time i get it again after sometime. Could you perhaps look in to the proteingeschoss class, because it says thats where the error is.
Hellotheremyfriend Hellotheremyfriend

2020/11/22

#
public class Leukozyte extends Actor
{
    
    
    public void act() 
    {
        
        
        move(5);
        rechts();
        links();
        fire();
        
    }  
    public void fire()
    {
        if(Greenfoot.mousePressed(null))
        {
            Proteingeschoss proteingeschoss = new Proteingeschoss();
            getWorld().addObject(proteingeschoss, getX(), getY());
            proteingeschoss.turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
        }
    }
    /**
     * Wenn der Key "d" dedrückt wird, dann dreht sich die Leukozyte im Uhrzeigersinn.
     */
    public void rechts()
    {
        if(Greenfoot.isKeyDown("d"))
        {
            turn(5);
        }
    }
    /**
     * Wenn der Key "a" gedrückt wird, dann dreht sich die Leukozyte gegen den Uhrzeigersinn.
     */
    public void links()
    {
        if(Greenfoot.isKeyDown("a"))
        {
            turn(-5);
           
        }
    }
   
           
}
public class Proteingeschoss extends Actor
{
     /**
     * Act - do whatever the Protonengeschoss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int speed = 7;
    public void act() 
    {
             
        
        turnToMouse();
        move(speed);
        destroyEnemies();
        atEdge();

    }    
    public void turnToMouse()
    {
        turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
    }
    
     /**
     * Durch die Methode "destroyEnemies()" werden Feinde bzw. feindliche Organismen zerstört
     */
    public void destroyEnemies()
    {
        if(isTouching(Mikrobe.class))
        {
            removeTouching(Mikrobe.class);
            getWorld().removeObject(this);
        }
        if(isTouching(Fungi.class))
        {
            removeTouching(Fungi.class);
            getWorld().removeObject(this);
        }
        if(isTouching(Bakterium.class))
        {
            removeTouching(Bakterium.class);
            getWorld().removeObject(this);
        }
            
    }
    public void atEdge()
    {
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
    
    
}
Hellotheremyfriend Hellotheremyfriend

2020/11/22

#
I have changed both of th codes now. And I get still get it.
danpost danpost

2020/11/22

#
If no mouse action takes place within an act cycle, Greenfoot.getMouseInfo() will return a null value. Check for a null value before trying to obtain the location of the mouse in the turnToMouse method.
danpost danpost

2020/11/22

#
In the destroyEnemies method, use if / else if / else if / ... Also, in the atEdge method, check for getWorld returning null before using isAtEdge().
Hellotheremyfriend Hellotheremyfriend

2020/11/22

#
I don’t quite understand, could you perhaps type it for me. Would really appreciate bro.
Hellotheremyfriend Hellotheremyfriend

2020/11/22

#
I am kinda new to programming in general having it in school for the first time.
danpost danpost

2020/11/22

#
Hellotheremyfriend wrote...
I don’t quite understand, could you perhaps type it for me. Would really appreciate bro.
if (Greenfoot.getMouseInfo() != null)
If this is true, then you can acquire mouse's x and y coordintes.
Hellotheremyfriend Hellotheremyfriend

2020/11/22

#
public class Proteingeschoss extends Actor
{
     /**
     * Act - do whatever the Protonengeschoss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int speed = 6;
    public void act() 
    {
        
        
        turnToMouse();
        move(speed);
        destroyEnemies();
        atEdge();

    }    
    public void turnToMouse()
    {
        if (Greenfoot.getMouseInfo() != null)
        {
        turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
    }
    }
    
     /**
     * Durch die Methode "destroyEnemies()" werden Feinde bzw. feindliche Organismen zerstört
     */
    public void destroyEnemies()
    {
        if(isTouching(Mikrobe.class))
        {
            removeTouching(Mikrobe.class);
            getWorld().removeObject(this);
        }
        
        
        if(isTouching(Fungi.class))
        {
            removeTouching(Fungi.class);
            getWorld().removeObject(this);
        }
    
        
        
        if(isTouching(Bakterium.class))
        {
            removeTouching(Bakterium.class);
            getWorld().removeObject(this);
        }
    }


    
    public void atEdge()
    {
        if(Greenfoot.getWorld() != null)
        {
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
    }
Hellotheremyfriend Hellotheremyfriend

2020/11/22

#
Is line 57 how you meant it to be because it returns an error for me.
danpost danpost

2020/11/22

#
Hellotheremyfriend wrote...
Is line 57 how you meant it to be because it returns an error for me.
As it should (return an error, that is). The getWorld method is not defined in the Greenfoot class. Also, it is not a static method -- meaning you cannot call it on a class name. It is a non-static method defined in the Actor class. No object need be explicitly given to execute the method on as it is implicitly executed on the actor for which the atEdge method is called for. You could use "this.getWorld"; but, "getWorld" will work as well.
Hellotheremyfriend Hellotheremyfriend

2020/11/22

#
I am very thankful for your help, but I have one question: Could you perhaps tell me why the object, which is shot, is always following my cursor? I would like to have it move to the direction my cursor faces,when I click, with a constant velocity.
Hellotheremyfriend Hellotheremyfriend

2020/11/22

#
public class Leukozyte extends Actor
{
    
    
    public void act() 
    {
        
        
        move(5);
        rechts();
        links();
        fire();
        
    }  
    public void fire()
    {
        if(Greenfoot.mousePressed(null))
        {
            Proteingeschoss proteingeschoss = new Proteingeschoss();
            getWorld().addObject(proteingeschoss, getX(), getY());
            proteingeschoss.turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
        }
    }
    /**
     * Wenn der Key "d" dedrückt wird, dann dreht sich die Leukozyte im Uhrzeigersinn.
     */
    public void rechts()
    {
        if(Greenfoot.isKeyDown("d"))
        {
            turn(5);
        }
    }
    /**
     * Wenn der Key "a" gedrückt wird, dann dreht sich die Leukozyte gegen den Uhrzeigersinn.
     */
    public void links()
    {
        if(Greenfoot.isKeyDown("a"))
        {
            turn(-5);
           
        }
    }
   
           
}
public class Proteingeschoss extends Actor
{
     /**
     * Act - do whatever the Protonengeschoss wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int speed = 6;
    public void act() 
    {
        
        
        turnToMouse();
        move(speed);
        destroyEnemies();
        atEdge();

    }    
    public void turnToMouse()
    {
        if (Greenfoot.getMouseInfo() != null)
        {
        turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
    }
    }
    
     /**
     * Durch die Methode "destroyEnemies()" werden Feinde bzw. feindliche Organismen zerstört
     */
    public void destroyEnemies()
    {
        if(isTouching(Mikrobe.class))
        {
            removeTouching(Mikrobe.class);
            getWorld().removeObject(this);
        }
        else 
        
        
        if(isTouching(Fungi.class))
        {
            removeTouching(Fungi.class);
            getWorld().removeObject(this);
        }
        else
        
        
        if(isTouching(Bakterium.class))
        {
            removeTouching(Bakterium.class);
            getWorld().removeObject(this);
        }
    }


    
    public void atEdge()
    {
        if(this.getWorld() != null)
        {
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
    }
    }
There are more replies on the next page.
1
2