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

2017/3/16

java.lang.NullPointerException

heyu heyu

2017/3/16

#
I recieve with following code an null pointer exeception why?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Die Klasse MyKara ist eine Unterklasse von Kara.
 * Sie erbt damit alle Methoden der Klasse Kara:
 * 
 * Aktionen:    move(), turnLeft(), turnRight(), putLeaf(), removeLeaf()
 * Sensoren:    onLeaf(), treeFront(), treeLeft(), treeRight(), mushroomFront()
 */
public class MyKara extends Kara
{
    /**
     * In der Methode 'act()' koennen die Befehle fuer Kara programmiert werden.
     * Die Befehle werden dort nacheinander aufgerufen, wenn man nach dem 
     * 'Uebersetzen' (=kompilieren) den 'Act'-Knopf drueckt.
     * Beim druecken des 'Run'-Knopfes wird diese Methode immer wieder ausgefuehrt.
     */
    
    public void bewegen(int x)
    {
        
        if(getX() != x - 1 && Greenfoot.isKeyDown("left"))
        {
            turnLeft(); 
            if(canMove())
            {
                move();
            }
            turnRight();
        }
        
        
        
        if(getX() != x - 1 && Greenfoot.isKeyDown("right"))
        {
            turnRight(); 
            if(canMove())
            {
                move(); 
            }
            turnLeft();
        }
    }
    
    
    
    BeweglicherTree treeTree = new BeweglicherTree();
   
    
    public void act() 
    {
        
        while(!treeFront())
        {
            bewegen(getX());
            treeTree.baumAct(getX());
        }
    }  
}
 import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class BeweglicherTree extends Actor
{
   public void beweglicheBäume(int xx)
    {
        int anzahlBeweglicherBäume = 5;
       
        Tree treeB[] = new Tree[anzahlBeweglicherBäume];
        for(int i = 0; i < anzahlBeweglicherBäume; i++)
        {
            treeB[i] = new Tree();
        }
        
        Baum baum[] = new Baum[anzahlBeweglicherBäume];        
        baum[0] = new Baum(2, 9);
        /*baum[1] = new Baum(2, 3);
        baum[2] = new Baum(2, 5);
        baum[3] = new Baum(2, 7);
        baum[4] = new Baum(2, 9);*/
        
        boolean karaVormBaum = false;
        
        do
        {
            for(int a = 0; a < anzahlBeweglicherBäume; a++)
            {
                karaVormBaum = false;
                
                if(baum[a].getLocationY() >= -1 && baum[a].getLocationY() <= 25)
                {
                    if(baum[a].getLocationX() + 1 == xx)
                    {
                        karaVormBaum = true;
                    }
                    if(baum[a].getLocationY() >= 0)
                    {
                        getWorld().removeObject(treeB[a]);
                    }
                    baum[a].setLocation(baum[a].getLocationX(), baum[a].getLocationY() + 1);
                    if(baum[a].getLocationY() <= 25)
                    {
                        getWorld().addObject(treeB[a],baum[a].getLocationX(),baum[a].getLocationY());
                    }   
                }
            }
            
        }while(!karaVormBaum);   
    }
   public void baumAct(int xx) 
   {
       beweglicheBäume(xx);
   }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Baum here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Baum extends Actor
{
    int xK;
    int yK;
    
    public Baum(int xK, int yK)
    {
        this.xK = xK;
        this.yK = yK;
    }
    
    public int getLocationX()
    {
        return xK;
    }
    
    public int getLocationY()
    {
        return yK;
    }
    
    public void setLocationY(int y, int x)
    {
        this.xK = x;
        this.yK = y;
    }
}
Can anyone help me?
danpost danpost

2017/3/16

#
Chang the value at line 7 to '1' instead of '5' (until you decide to uncomment lines 17 through 20).
davmac davmac

2017/3/16

#
It would make it easier to spot the problem if you would post the complete stack trace, or at least specify which line the exception occurs on.
heyu heyu

2017/3/16

#
@danpost yes you are right, but that isn´t the problem This is what i recieve after I hit the run Button: java.lang.NullPointerException at BeweglicherTree.beweglicheBäume(BeweglicherTree.java:32) at BeweglicherTree.act(BeweglicherTree.java:46) at MyKara.act(MyKara.java:56) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
heyu heyu

2017/3/16

#
But i changed the code, so it was line 38 and not 32 and line 52 and not 46.
Super_Hippo Super_Hippo

2017/3/16

#
You create a moving tree (BeweglicherTree) in your MyKara class and then try use methods on this object which require the object to be in the world, but it was never added. What exactly are trying to do? If you just want to look for trees around MyKara, you should not create a new tree in the class.
heyu heyu

2017/3/16

#
I try to make a game. The player moves an animal named kara with the keyboard and trees will run down from the top (or kara is running towards the trees) and it is over as soon as kara hits a tree.
Super_Hippo Super_Hippo

2017/3/16

#
I think you should just have something like this:
//moving tree
public void act()
{
    setLocation(getX(), getY()+1);
}
//Kara
public void act()
{
    //move around
    
    if (isTouching(Tree.class))
    {
        //end the game
        Greenfoot.stop();
    }
}
heyu heyu

2017/3/17

#
And where can i add the trees and where can i remove them
Super_Hippo Super_Hippo

2017/3/17

#
Add the trees from your world's act method:
//in your world
private int treeSpawnCounter = 10;

public MyWorld() //replace with your world name
{
    super(...., false); //add the false at the end
    //...
}

public void act()
{
    if (--treeSpawnCounter==0)
    {
        addObject(new Tree(), Greenfoot.getRandomNumber(getWidth()), -10);
        treeSpawnCounter = 50 + Greenfoot.getRandomNumber(50);
    }
}
To remove the trees:
//moving tree
public void act()
{
    setLocation(getX(), getY()+1);
    if (getY() > getWorld().getHeight()+10)
    {
        getWorld().removeObject(this);
    }
}
heyu heyu

2017/3/19

#
Well I solved that problem, I tried to add Trees in an actor class not in a world class. Thanks for the help.
You need to login to post a reply.