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

2015/4/19

Complete failure bug :(

Salcrainia Salcrainia

2015/4/19

#
So this one I have no idea as to what is going on but the error occurs when I click the button which is located in a different level with no code in it other than adding the button to it so I doubt that would be the reason that the error is being caused. Any help would be appreciated and if code is needed let me know what I missed out :)
java.lang.NullPointerException
	at greenfoot.World.addObject(World.java:396)
	at Level_1.<init>(Level_1.java:6)
	at Button_Play.act(Button_Play.java:9)
	at greenfoot.core.Simulation.actActor(Simulation.java:594)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
import greenfoot.*;
public class Level_1 extends Level
{    
    public Level_1()
    {         
        addObject(Level.getPlayer(), 54, 356);
        
        addObject(Level.getScore(), 40, 20);
        
        addObject(Level.getLives(), 120, 20);
        
        prepare();
    }
     
    public void prepare()
    {              
        flr2 floor1 = new flr2();
        addObject(floor1, 16, 385);
 
        flr2 floor2 = new flr2();
        addObject(floor2, 48, 385);
 
        flr2 floor3 = new flr2();
        addObject(floor3, 80, 385);
 
        flr2 floor4 = new flr2();
        addObject(floor4, 112, 385);
 
        flr2 floor5 = new flr2();
        addObject(floor5, 144, 385);
 
        flr2 floor6 = new flr2();
        addObject(floor6, 176, 385);
 
        flr2 floor7 = new flr2();
        addObject(floor7, 208, 385);
 
        flr2 floor8 = new flr2();
        addObject(floor8, 240, 385);
 
        flr2 floor9 = new flr2();
        addObject(floor9, 272, 385);
 
        flr2 floor10 = new flr2();
        addObject(floor10, 304, 385);
 
        flr2 floor11 = new flr2();
        addObject(floor11, 336, 385);
 
        flr2 floor12 = new flr2();
        addObject(floor12, 368, 385);
 
        flr2 floor13 = new flr2();
        addObject(floor13, 400, 385);
 
        flr2 floor14 = new flr2();
        addObject(floor14, 432, 385);
 
        flr2 floor15 = new flr2();
        addObject(floor15, 464, 385);
 
        flr2 floor16 = new flr2();
        addObject(floor16, 496, 385);
 
        flr2 floor17 = new flr2();
        addObject(floor17, 528, 385);
 
        flr2 floor18 = new flr2();
        addObject(floor18, 560, 385);
 
        flr2 floor19 = new flr2();
        addObject(floor19, 592, 385);
 
        Coin coin = new Coin();
        addObject(coin, 220, 347);
 
        Slime slime = new Slime();
        addObject(slime, 429, 336);
 
        Slime slime2 = new Slime();
        addObject(slime2, 523, 339);
        removeObject(slime);
        removeObject(slime2);
        removeObject(coin);
        flr2 flr220 = new flr2();
        addObject(flr220, 560, 307);
        flr2 flr221 = new flr2();
        addObject(flr221, 528, 307);
        flr2 flr222 = new flr2();
        addObject(flr222, 496, 308);
        flr221.setLocation(528, 307);
        flr222.setLocation(496, 307);
        Coin coin2 = new Coin();
        addObject(coin2, 563, 274);
        Ghost ghost = new Ghost();
        addObject(ghost, 498, 279);
        flr2 flr223 = new flr2();
        addObject(flr223, 446, 248);
        flr2 flr224 = new flr2();
        addObject(flr224, 414, 248);
        flr2 flr225 = new flr2();
        addObject(flr225, 510, 181);
        flr225.setLocation(513, 171);
        Key key = new Key();
        addObject(key, 514, 133);
        flr2 flr226 = new flr2();
        addObject(flr226, 31, 306);
        flr2 flr227 = new flr2();
        addObject(flr227, 62, 307);
        flr2 flr228 = new flr2();
        addObject(flr228, 97, 307);
        flr227.setLocation(63, 306);
        flr228.setLocation(95, 306);
        flr2 flr229 = new flr2();
        addObject(flr229, 130, 220);
        flr229.setLocation(152, 227);
        flr2 flr230 = new flr2();
        addObject(flr230, 87, 172);
        flr2 flr231 = new flr2();
        addObject(flr231, 57, 173);
        flr2 flr232 = new flr2();
        addObject(flr232, 250, 132);
        flr2 flr233 = new flr2();
        addObject(flr233, 282, 133);
        flr2 flr234 = new flr2();
        addObject(flr234, 328, 132);
        flr233.setLocation(282, 132);
        flr234.setLocation(314, 132);
        flr229.setLocation(181, 234);
        Loot loot = new Loot();
        addObject(loot, 54, 157);
        flr231.setLocation(55, 188);
        flr230.setLocation(87, 189);
        flr230.setLocation(87, 188);
        Slime slime3 = new Slime();
        addObject(slime3, 87, 162);
        Troll troll = new Troll();
        addObject(troll, 33, 280);
        troll.setLocation(32, 275);
        troll.setLocation(31, 278);
        Troll troll2 = new Troll();
        addObject(troll2, 212, 356);
        Ghost ghost2 = new Ghost();
        addObject(ghost2, 356, 356);
        ghost2.setLocation(354, 358);
        Door door = new Door();
        addObject(door, 282, 89);
        removeObject(loot);
        Coin coin3 = new Coin();
        addObject(coin3, 56, 157);
    }
}
import greenfoot.*;

public class Button_Play extends Actor
{
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(new Level_1());
        }
    }    
}
import greenfoot.*;
  
public class Level extends World
{
    private static Player thePlayer;
    private static Score theScore;
    private static Lives theLives;
      
    public Level()
    {    
        super(600, 400, 1); 
        if ("Level".equals(getClass().getName()))
        {
            thePlayer = new Player();
            theScore = new Score();
            theLives = new Lives();
            Greenfoot.setWorld(new Level_Home());
        }
    }
         
    public static Score getScore()
    {
        return theScore;
    }
      
    public static Lives getLives()
    {
        return theLives;
    }
 
    public static Player getPlayer()
    {
        return thePlayer;
    }
}
import greenfoot.*;
import java.awt.Color;
 
public class Player extends Actor
{
    private int frame = 0;
    private int direction = 1;
    private int jumpStrength = 12;
    private boolean jump = false;
    private int gravityStrength = 1;
    private int vSpeed = 0;
    private int animationCounter = 0;
    private int attackCounter = 0;
    private int score = 0;
    private int lives = 1;
    private boolean hasKey = false;
    private Color transparent = new Color(0, 0, 0, 0);
     
    /**
     * These are all of the variables which are defined on the inital load up of the game
     */
    public Player()
    {
        setImage("Hero_1.png");
    }
     
    public void act() 
    {
        attackCounter --;
        animationCounter++;
        Movement();
        floorCollision();
        checkIfFalling();
        collectLoot();
        killed();
        if(getWorld() != null){surfaceAbove();}
        if(getWorld() != null){getKey();}
        if(getWorld() != null){openDoor();}
        if(getWorld() != null){openDoor2();}
        if(getWorld() != null){openDoor3();}        
    }  
     
    /**
     * Detects interactions
     */
    public boolean detect(Class cls)
    {
        Actor detect = getOneObjectAtOffset(0, 0, cls);
        return detect != null;
    }
     
    /**
     * Removes interacted object
     */
    public void remove(Class cls)
    {
        Actor remove = getOneObjectAtOffset(0, 0, cls);
        if(remove != null)
        {
            getWorld().removeObject(remove);
        }
    }
     
    public void collectLoot()
    {
        if(detect(Coin.class))
        {
            score = 10;
            addScore();
            remove(Coin.class);
        }
    }
     
    public void getKey()
    {
        if(detect(Key.class))
        {
            remove(Key.class);
            hasKey = true;
        }
    }
     
    /**
     * This is the code that I have been testing the system with
     */
    public void openDoor()
    {
        if(detect(Door.class) && hasKey == true)
        {
            score = 25;
            addScore();
            World world = new Level_2();
            Score score = new Score();
            Greenfoot.setWorld(world);
            hasKey = false;
        }
    }
     
    public void openDoor2()
    {
        if(detect(Door2.class) && hasKey == true)
        {
            score = 25;
            addScore();
            World world = new Level_3();
            Score score = new Score();
            Greenfoot.setWorld(world);
            hasKey = false;
        }
    }
     
    public void openDoor3()
    {
        if(detect(Door3.class) && hasKey == true)
        {
            score = 25;
            addScore();
            World world = new Level_Home();
            Score score = new Score();
            Greenfoot.setWorld(world);
            hasKey = false;
            Greenfoot.stop();
        }
    }
     
    /**
     * This checks whether or not the player is in contact with the floor
     */
    public boolean floorCollision()
    {
        int playerHeight = getImage().getHeight();
        int fallDistance = (int) (playerHeight/2) + 10;
        Actor surface = getOneObjectAtOffset(0, getImage().getHeight()/2, Surface.class);
        if(surface == null)
        {
            jump = true;
            return false;
        }        
        else
        {
            Gravity(surface);
            return true;
        }
    }
         
    /**
     * This causes the player to fall if they're jumping or in the air
     */
    public void Fall()
    {
        setLocation(getX(), getY() + (int) vSpeed);
        if (vSpeed <= 9)
        {
            vSpeed = vSpeed + gravityStrength;
        }
        jump = true;
    }
     
    /**
     * This makes the player jump
     */
    public void Jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jump = true;
        Fall();
    }
     
    public void Gravity(Actor surface)
    {
        int surfaceHeight = surface.getImage().getHeight();
        int newY = surface.getY() - (surfaceHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
        jump = false;
    }
     
    private void checkIfFalling()
    {
        if(floorCollision())
        {
            vSpeed = 0;
        }
        else
        {
            Fall();
        }
    }
    
    public void Movement()
    {
        if(Greenfoot.isKeyDown("d"))
        {
            move(3);
            direction = 1;
            if(animationCounter % 4 == 0)
            {
                Animation();
            }
        }
         
        if(Greenfoot.isKeyDown("a"))
        {
            move(-3);
            direction = -1;
            if(animationCounter % 4 == 0)
            {
                Animation();
                Turn();
            }
        }
         
        if(Greenfoot.isKeyDown("w") && jump == false)
        {
            Jump();
        }
         
        if(Greenfoot.isKeyDown("space") && jump == false && attackCounter <= 0)
        {
            attackCounter = 40;
            if(direction == 1)
            {
                Attack_Right();
            }
            else if(direction == -1)
            {
                Attack_Left();
            }
        }        
    }
     
    /**
     * This causes the sprite to flip horizontally so they walk towards the correct direction
     */
    public void Turn()
    { 
        getImage().mirrorHorizontally();
    }
     
    /**
     * This is what causes the animation to play when the playes is walking
     */
    public void Animation()
    {
        frame++;
        if(frame == 0){setImage("Hero_1.png");}
        else if(frame == 1){setImage("Hero_2.png");}
        else if(frame == 2){setImage("Hero_3.png");}
        else if(frame == 3){setImage("Hero_4.png");}
        else if(frame == 4){setImage("Hero_5.png");}
        else if(frame == 5){setImage("Hero_6.png");}
        else if(frame == 6){setImage("Hero_7.png");}
        else if(frame == 7){setImage("Hero_8.png");
        frame = 0;
        }
    }
     
    public void Attack_Right()
    {    
        getWorld().addObject(new Attack_Right(), getX(), getY());
    }   
     
    public void Attack_Left()
    {    
        getWorld().addObject(new Attack_Left(), getX(), getY());
    }   
     
    /**
     * This causes the mobs to kill the player upon collision and forces the player to lose a life
     */
    public void killed()
    {
        if(detect(Mob.class))
        {
            loseLife();
            setLocation(30, 335);
        }
    }
 
    public void bopHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
    }
 
    public boolean surfaceAbove()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/-2);
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Surface.class);
        if(ceiling != null)
        {
            vSpeed = 1;
            bopHead(ceiling);
            return true;
        }
        else
        {
            return false;
        }
    }
     
    /**
     * Increases score to the score counter
     */
    public void loseLife()
    {
        Level lvl = (Level) getWorld();
        Lives counter = lvl.getLives();
        counter.loseLives(lives);
    }
     
    /**
     * Increases score to the score counter
     */
    public void addScore()
    {
        Level lvl = (Level) getWorld();
        Score counter = lvl.getScore();
        counter.addScore(score);
    }
     
    public int getScore()
    {
        return score;
    }
     
    public int getLives()
    {
        return lives;
    }
}
Super_Hippo Super_Hippo

2015/4/19

#
I don't know in which world the game starts, but in your case it has to start in world "Level" (see line 12 in the Level class). If this isn't the case, change the "Level" to "Level_1" for example.
lordhershey lordhershey

2015/4/19

#
the call addObject(Level.getPlayer(), 54, 356); in the Level_1 constructor is the issue - the Level.getPlayer() static call is returning a null pointer since it is not set. I think a quick get around this might be in the Level class
//Change:
    private static Player thePlayer;
    private static Score theScore;
    private static Lives theLives;
       
    public Level()
    {    
        super(600, 400, 1); 
        if ("Level".equals(getClass().getName()))
        {
            thePlayer = new Player();
            theScore = new Score();
            theLives = new Lives();
            Greenfoot.setWorld(new Level_Home());
        }
    }


//To This:

    private static Player thePlayer;
    private static Score theScore;
    private static Lives theLives;
       
    //This is initialized upon class loading
    static{
            thePlayer = new Player();
            theScore = new Score();
            theLives = new Lives();
    }

    public Level()
    {    
        super(600, 400, 1);

        /*
         You might kill this block too - It makes no sense as to why you would be setting the world in the constructor... If you do not want Level to be called directly - do not call it directly or make the class abstract. Note code to be killed is in this comment.
        
        if ("Level".equals(getClass().getName()))
        {
            Greenfoot.setWorld(new Level_Home());
        }
       */
    }
you should also change this:
Add this to the Level_1 constructor:
 public Level_1()
    {         
        super();

        addObject(Level.getPlayer(), 54, 356);
         
        addObject(Level.getScore(), 40, 20);

Super_Hippo Super_Hippo

2015/4/19

#
Isn't this
    private static Player thePlayer;
    private static Score theScore;
    private static Lives theLives;
        
    //This is initialized upon class loading
    static{
            thePlayer = new Player();
            theScore = new Score();
            theLives = new Lives();
    }
the same as
private static Player thePlayer = new Player();
private static Score theScore = new Score();
private static Lives theLives new Lives();
? Or is this 'static { }' also executed when clicking on reset?
lordhershey lordhershey

2015/4/19

#
that is the same - I just do it the first way so I can see the definitions by them selves - in the static{} block you can do loops and the like for more complex structures.
Salcrainia Salcrainia

2015/4/20

#
Thanks a lot, it worked fine, my game is basically complete now, just need to clean up the level codes to not have so many setLocations :P
lordhershey lordhershey

2015/4/20

#
You are welcome. There is nothing wrong with setLocations - whatever it takes to make it work. :D
Salcrainia Salcrainia

2015/4/20

#
Just testing it again now cause I needed to get some screenshots of it but now the score isn't carrying over :/ any ideas?
Salcrainia Salcrainia

2015/4/20

#
Never mind it was an error on my end when trying to clear the level code of all the junk, deleted something by accident :S my bad :P
You need to login to post a reply.