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

2017/4/3

java.lang.NullPointerException

Alex12 Alex12

2017/4/3

#
I got a problem right here.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class andreas here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class andreas extends Actor
{
    /**
     * Act - do whatever the andreas wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(andreas.class != null && getWorld() != null)
        {

            setLocation(getX()-3,getY());

            checkTouching();

        }

    }    

    public void checkTouching()
    {
        if(isTouching(explosion.class))
        {
            removeTouching(explosion.class);
            MyWorld myWorld = (MyWorld)getWorld();
            myWorld.addScore(2);
            getWorld().addObject(new missile(), 35, 375);
            Greenfoot.playSound("sounds/andreas.mp3");
            getWorld().removeObject(this);
            return;
        }
        if(isTouching(andreas.class))
        {

            getWorld().addObject(new andreas(),1550, Greenfoot.getRandomNumber(900));
            getWorld().removeObject(this);
            return;
        } 

    }

}
World class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.awt.Color;
import java.awt.Font;
/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    public int img;
    private int age=0;
    public int score;
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */

    GreenfootImage ge2 = new GreenfootImage("images/landschaft2.png");
    GreenfootImage ge3 = new GreenfootImage("images/landschaft3.png");
    GreenfootImage ge4 = new GreenfootImage("images/landschaft4.png");
    GreenfootImage ge5 = new GreenfootImage("images/landschaft5.png");
    GreenfootImage ge6 = new GreenfootImage("images/landschaft6.png");
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1600, 900, 1); 
        imgset();
        prepare();
        score = 0;
        showScore();

    }

    public void imgset()
    {

        age++;

        if (age<2)
        {
            img = (Greenfoot.getRandomNumber(5));

            if (img == (0))
            {
                this.getBackground().drawImage(ge2,0,0);
            }
            if (img == (1))
            {
                this.getBackground().drawImage(ge3,0,0);
            }
            if (img == (2))
            {
                this.getBackground().drawImage(ge4,0,0);
            }
            if (img == (3))
            {
                this.getBackground().drawImage(ge5,0,0);
            }
            if (img == (4))
            {
                this.getBackground().drawImage(ge6,0,0);
            }
            age = age+1;

        }

    }

    public void act()
    {
        if (Greenfoot.getRandomNumber(150) < 1)
        {
            addObject(new andreas(), 1550, Greenfoot.getRandomNumber(900));
        }
        if (Greenfoot.getRandomNumber(150) < 1)
        {
            addObject(new davis(), 1550, Greenfoot.getRandomNumber(900));
        }
    }

    public void prepare()
    {
        missile Missile = new missile();

        addObject(new missile(),35,375);

        Abschussrampe abschussrampe = new Abschussrampe();
        addObject(new Abschussrampe(),63,370);

    }

    private void showScore()
    {
        showText("Score: " + score, 100, 50);
    }

    public void addScore(int points)
    {
        score = score + points;
        showScore();
        if (score >= 12) 
        {
            pointcheck10();
            
            
        }
        if (Greenfoot.isKeyDown("2"))
        {
            score = score + 20;
        }
        
    }

    public void pointcheck10()
    {
       removeObjects(getObjects(null));
        addObject(new TextField(200, 40, Color.black, Color.red ,"Level 1 complete"), 800, 450);
        addObject(new TextField(200, 40, Color.magenta, Color.black ,"Explosiondamage"), 500, 800);
        addObject(new TextField(200, 40, Color.red, Color.black ,"Faster Rocket"), 800, 800);
        addObject(new TextField(200, 40, Color.blue, Color.black ,"Slower Targets"), 1100, 800);
        Greenfoot.delay(2);
        
    }
    
}
As i execute the code and reach 12 points everything works fine but I get this error: java.lang.NullPointerException at andreas.checkTouching(davis.java:31) at andreas.act(davis.java:21) 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) It probably has to do with me removing all objects and the code still trying to be executed . I tried to remove that by using the line
if(andreas.class != null && getWorld() != null)
but still no changes. I really need help here.
danpost danpost

2017/4/3

#
I am a bit confused by the error trace you are getting. Do you have a class called 'davis'? if so, what classes lead to it (list all from World, Actor or Object) and please show it codes.
Alex12 Alex12

2017/4/3

#
Yep, an actor class is called davis. davis code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class davis here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class davis extends Actor
{
    
    /**
     * Act - do whatever the davis wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(this != null && this.getWorld() != null)
        {

            setLocation(getX()-2,getY());
            checkTouching();
        }
    }    

    public void checkTouching()
    {
        if(isTouching(explosion.class))
        {
            if(this != null && this.getWorld() != null)
            {
                removeTouching(explosion.class);
                MyWorld myWorld = (MyWorld)getWorld();
                myWorld.addScore(1);
                getWorld().addObject(new missile(), 35, 375);
                Greenfoot.playSound("sounds/1&1.mp3");
                getWorld().removeObject(this);
                return;
            }
        }

    }
    
}
andreas code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class andreas here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class andreas extends Actor
{
    /**
     * Act - do whatever the andreas wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(andreas.class != null && getWorld() != null)
        {

            setLocation(getX()-3,getY());

            checkTouching();

        }

    }    

    public void checkTouching()
    {
        if(andreas.class != null && this.getWorld() != null)
        {
            if(isTouching(explosion.class))
            {
                removeTouching(explosion.class);
                MyWorld myWorld = (MyWorld)getWorld();
                myWorld.addScore(2);
                getWorld().addObject(new missile(), 35, 375);
                Greenfoot.playSound("sounds/andreas.mp3");
                getWorld().removeObject(this);
                return;
            }
            if(isTouching(andreas.class))
            {

                getWorld().addObject(new andreas(),1550, Greenfoot.getRandomNumber(900));
                getWorld().removeObject(this);
                return;
            } 
        }
    }

}
missile class = the playable character.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class missile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class missile extends Actor
{
    private int speedForward = 7;
    private int turnLeft = -5;
    private int turnRight = 5;
    public static int missileX = 0;
    public static int missileY = 0;
    public int vSpeed = 0;
    public double accelaration = 1;

    /**
     * Act - do whatever the missile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        start();
        xyValues();
        rocketX();
        rocketY();
        positioning();
        pointcheck10();
        checkTouching();

    }

   public void xyValues()
    {
        missileX = getX();
        missileY = getY();
    }

    public static int rocketX()
    {
        return missileX;
    }

    public static int rocketY()
    {
        return missileY;
    }

    public void start()
    {
        if (Greenfoot.isKeyDown("w"))
        {
            move(speedForward);
            vSpeed = 0;
        }
        else
        {
            fall();
        }
        if (Greenfoot.isKeyDown("a"))
        {
            turn(turnLeft);

        }
        if (Greenfoot.isKeyDown("d"))
        {
            turn(turnRight);

        }
    }

    public void fall()
    {
        if (Greenfoot.getRandomNumber(100)<50)
        {
            setLocation(getX(),getY() + vSpeed);
            vSpeed = vSpeed + (int)accelaration;

        }
        else
        {
            fall2();
        }
    }

    public void fall2()
    {
        if(getX()<350)
        {
            vSpeed = 2;
        }
        if(getX()<200)
        {
            vSpeed = 4;
        }
        if(getX()<350)
        {
            vSpeed = 6;
        }

    }

    public void positioning()
    {
        if(getX() == 35 && getY() == 375)
        {
            vSpeed = 0;
        }
    }

    public void checkTouching()
    {
        if (isTouching(andreas.class))
        {

            getWorld().addObject(new explosion(),getX()+20,getY()-20);
            getWorld().removeObject(this);
            
            return;
        }
        if (isTouching(davis.class))
        {

            getWorld().addObject(new explosion(),getX()+20,getY()-20);
            getWorld().removeObject(this);

        }
    }

    public void newMissile()
    {
        if(Greenfoot.isKeyDown("n"))
        {
            getWorld().addObject(new missile(),35,375);
            
        }
    }
    public void pointcheck10()
    {
        if(Greenfoot.isKeyDown("2"))
        {
            int speedForward = 10;
            getWorld().removeObjects(getWorld().getObjects(null));
        }
    }
}

World code is listed above already.
danpost danpost

2017/4/3

#
I think maybe your greenfoot.project file is corrupted. For some reason, the error trace is telling me that it is running code (the act method) in the davis class for an andreas object, which seems quite odd, to tell you the truth. Never seen anything like it! My suggestion is to start a new project, create the needed classes and copy/paste the codes from this project into it. Then delete this project. Hopefully that will fix your problem.
Alex12 Alex12

2017/4/3

#
Thank you for your help anyways. Have a good one.
You need to login to post a reply.