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

2020/3/16

NullPointerException when trying to speed up paddle

CodesForFun CodesForFun

2020/3/16

#
I'm trying to make a power-up that permanently speeds up a paddle that you move around. Originally I had a problem with the paddle infinitely speeding up, so I wanted to access the speeding up method from the power-up itself. So I checked the tutorial on how to access an object from another object, which produced no errors. However, the moment I hit the power-up, an error was thrown: java.lang.NullPointerException at PaddleSpeedUp.activatePowerUp(PaddleSpeedUp.java:29) at PaddleSpeedUp.removePowerUp(PaddleSpeedUp.java:23) at PaddleSpeedUp.act(PaddleSpeedUp.java:18) Here's the code for the Paddle:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Paddle extends Actor
{
    public int paddleSpeed = 10;
    int speedy;
    int i;
    public void act()
    {
        movement();
        speedy = PaddleSpeedUp.speedUp;
    }
    public void movement(){
        if(Greenfoot.isKeyDown("left")){
            setLocation(getX()-paddleSpeed, getY());
        }
        if(Greenfoot.isKeyDown("right")){
            setLocation(getX()+paddleSpeed, getY());
        }
    }
    public void updateSpeed(){
        paddleSpeed = paddleSpeed + speedy;
    }
}
This is the Power-up's:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class PaddleSpeedUp extends Actor
{
    public static int speedUp;
    /**
     * Act - do whatever the Paddle_Speed_Up wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        removePowerUp();
    }    
    public void removePowerUp(){
        if(isTouching(Ball.class)){
            getWorld().removeObject(this);
            activatePowerUp();
        }
    }
    public void activatePowerUp(){
        speedUp += 1;
        firstLevel theWorld = (firstLevel) getWorld();
        Paddle paddle = theWorld.getPaddle();
        paddle.updateSpeed();
    }
    public static int getSpeedUp(){
        return speedUp;
    }
}
And the code for the World:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class firstLevel extends World
{
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public int spawnTimer;
    private int timerThing = 600;
    private Paddle thePaddle;
    GreenfootSound backgroundMusic = new GreenfootSound("03_Otherworldly_Corridor.mp3");
    public firstLevel()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        //backgroundMusic.playLoop();
        prepare();
    }
    public void act(){
        randomlySpawn();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Ball ball = new Ball();
        addObject(ball,239,144);
        thePaddle = new Paddle();
        addObject(thePaddle,243,296);
        Ground ground = new Ground();
        addObject(ground,300,399);
    }
    public void randomlySpawn(){
        spawnTimer = (spawnTimer+1)%timerThing; // repeat every 10 seconds (about)
        if (spawnTimer == 0) // at each timer reset
        {
            timerThing = getRandomNumber(10, 500);
            addObject(new PaddleSpeedUp(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(290));
        }
    }
    public int getRandomNumber(int start, int end){
        int normal = Greenfoot.getRandomNumber(end-start+1);
        return normal+start;
    }
    public Paddle getPaddle(){
        return thePaddle;
    }
}
danpost danpost

2020/3/16

#
Switch lines 16 and 17 in PaddleSpeedUp class. The activatePowerUp method requires the PaddleSpeedUp instance be in the world to execute properly.
CodesForFun CodesForFun

2020/3/16

#
I don't know why I didn't think of that. Thank you.
CodesForFun CodesForFun

2020/3/17

#
danpost wrote...
Switch lines 16 and 17 in PaddleSpeedUp class. The activatePowerUp method requires the PaddleSpeedUp instance be in the world to execute properly.
Okay, so this is a bit embarrassing but I'm experiencing the same problem again but with a different power-up. I checked to see if the code to activate it ran before the code to delete the power-up, and it did. This power-up (more of a power-down really) creates a fog that obscures vision, making it difficult to see. I made it so that the ball calls the fog object so that the fog appears. I have the fog roll in from the right with a bunch of moves queued up with a while() statement. java.lang.NullPointerException at FogBall.activatePowerUp(FogBall.java:27) at FogBall.removePowerUp(FogBall.java:20) at FogBall.act(FogBall.java:16) This is the code for the ball that activates it:
public class FogBall extends Actor
{
    /**
     * Act - do whatever the Paddle_Speed_Up wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        removePowerUp();
    }    
    public void removePowerUp(){
        if(isTouching(Ball.class)){
            activatePowerUp();
            getWorld().removeObject(this);
        }
    }
    public void activatePowerUp(){
        firstLevel theWorld = (firstLevel) getWorld();
        Fog foggy = theWorld.getFog();
        foggy.summonFog();
    }
}
code for the fog itself:
public class Fog extends Actor
{
    public boolean foggy;
    public int foggyTimer;
    int i;
    /**
     * Act - do whatever the Fog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        fogTimer();
    }
    public void summonFog(){
        firstLevel theWorld = (firstLevel) getWorld();
        fogRollIn();
        foggy = true;
    }
    public void fogTimer(){
        if(foggy){
            foggyTimer++;
            if(((foggyTimer+1)%getRandomNumber(600,1200)==0)){
                getWorld().removeObject(this);
                foggy = false;
            }
        }
    }
    public void fogRollIn(){
        while(i<60){
            i++;
            setLocation(getX()-10, getY());
        }
    }
    public int getRandomNumber(int start, int end){
        int normal = Greenfoot.getRandomNumber(end-start+1);
        return normal+start;
    }
}
danpost danpost

2020/3/17

#
firstLevel class codes, please.
CodesForFun CodesForFun

2020/3/17

#
right, sorry.
public class firstLevel extends World
{
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public int score;
    public int i;
    public int scoreTick;
    public int spawnTimerSpeedUp;
    public int spawnTimerSpeedDown;
    public int spawnTimerBallSpeedUp;
    public int spawnTimerBallSpeedDown;
    public int spawnTimerScoreUp;
    public int spawnTimerScoreDown;
    public int spawnTimerReverseControls;
    public int spawnTimerTransparentBall;
    private int timerThingSpeedUp = getRandomNumber(755, 1000);
    private int timerThingSpeedDown = getRandomNumber(768, 1100);
    private int timerThingBallSpeedUp = getRandomNumber(843, 1231);
    private int timerThingBallSpeedDown = getRandomNumber(752, 1500);
    private int timerThingScoreUp = getRandomNumber(600, 1200);
    private int timerThingScoreDown = getRandomNumber(832, 1311);
    private int timerThingReverseControls = getRandomNumber(713, 912);
    private int timerThingTransparentBall = getRandomNumber(900, 1321);
    private Paddle thePaddle;
    private Ball theBall;
    private Fog theFog;
    private Counter scoreCounter;
    GreenfootSound backgroundMusic = new GreenfootSound("03_Otherworldly_Corridor.mp3");
    public firstLevel()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        scoreCounter = new Counter("Score: ");
        addObject(scoreCounter, 300, 20);
        //backgroundMusic.playLoop();
        prepare();
    }
    public void act(){
        randomlySpawnSpeedUp();
        randomlySpawnSpeedDown();
        randomlySpawnBallSpeedUp();
        randomlySpawnBallSpeedDown();
        randomlySpawnScoreUp();
        randomlySpawnScoreDown();
        randomlySpawnReverseControls();
        randomlySpawnTransparentBall();
        scoreTick++;
        if(scoreTick%60==0){
            score++;
        }
        scoreCounter.setValue(score);
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        theBall = new Ball();
        addObject(theBall,300,144);
        thePaddle = new Paddle();
        addObject(thePaddle,300,337);
        Ground ground = new Ground();
        addObject(ground,300,399);
    }

    public void randomlySpawnSpeedUp(){
        spawnTimerSpeedUp = (spawnTimerSpeedUp+1)%timerThingSpeedUp; // repeat every 10 seconds (about)
        if (spawnTimerSpeedUp == 0) // at each timer reset
        {
            timerThingSpeedUp = getRandomNumber(755, 1000);
            addObject(new PaddleSpeedUp(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(290));
        }
    }
    public void randomlySpawnSpeedDown(){
        spawnTimerSpeedDown = (spawnTimerSpeedDown+1)%timerThingSpeedDown; // repeat every 10 seconds (about)
        if (spawnTimerSpeedDown == 0) // at each timer reset
        {
            timerThingSpeedDown = getRandomNumber(768, 1100);
            addObject(new PaddleSpeedDown(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(290));
        }
    }
    public void randomlySpawnBallSpeedUp(){
        spawnTimerBallSpeedUp = (spawnTimerBallSpeedUp+1)%timerThingBallSpeedUp; // repeat every 10 seconds (about)
        if (spawnTimerBallSpeedUp == 0) // at each timer reset
        {
            timerThingBallSpeedUp = getRandomNumber(843, 1231);
            addObject(new BallSpeedUp(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(290));
        }
    }
    public void randomlySpawnBallSpeedDown(){
        spawnTimerBallSpeedDown = (spawnTimerBallSpeedDown+1)%timerThingBallSpeedDown; // repeat every 10 seconds (about)
        if (spawnTimerBallSpeedDown == 0) // at each timer reset
        {
            timerThingBallSpeedDown = getRandomNumber(752, 1500);
            addObject(new BallSpeedDown(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(290));
        }
    }
    public void randomlySpawnScoreUp(){
        spawnTimerScoreUp = (spawnTimerScoreUp+1)%timerThingScoreUp; // repeat every 10 seconds (about)
        if (spawnTimerScoreUp == 0) // at each timer reset
        {
            timerThingScoreUp = getRandomNumber(600, 1200);
            addObject(new PlusFivePoints(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(290));
        }
    }
    public void randomlySpawnScoreDown(){
        spawnTimerScoreUp = (spawnTimerScoreUp+1)%timerThingScoreUp; // repeat every 10 seconds (about)
        if (spawnTimerScoreUp == 0) // at each timer reset
        {
            timerThingScoreUp = getRandomNumber(832, 1311);
            addObject(new PlusFivePoints(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(290));
        }
    }
    public void randomlySpawnReverseControls(){
        spawnTimerReverseControls = (spawnTimerReverseControls+1)%timerThingReverseControls; // repeat every 10 seconds (about)
        if (spawnTimerReverseControls == 0) // at each timer reset
        {
            timerThingReverseControls = getRandomNumber(713, 912);
            addObject(new ReverseControls(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(290));
        }
    }
    public void randomlySpawnTransparentBall(){
        spawnTimerTransparentBall = (spawnTimerTransparentBall+1)%timerThingTransparentBall; // repeat every 10 seconds (about)
        if (spawnTimerTransparentBall == 0) // at each timer reset
        {
            timerThingTransparentBall = getRandomNumber(900, 1321);
            addObject(new TransparentBall(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(290));
        }
    }
    public int getRandomNumber(int start, int end){
        int normal = Greenfoot.getRandomNumber(end-start+1);
        return normal+start;
    }
    public Paddle getPaddle(){
        return thePaddle;
    }
    public Ball getBall(){
        return theBall;
    }
    public Counter getCounter(){
        return scoreCounter;
    }
    public Fog getFog(){
        return theFog;
    }
}
danpost danpost

2020/3/17

#
Where is a Fog object being created?
CodesForFun CodesForFun

2020/3/17

#
danpost wrote...
Where is a Fog object being created?
Oh dear, I'm sorry. I tried fixing the code myself and forgot to put it into its earlier, more stable state. Luckily only Fog was affected by it. Here's what the code originally looked like before I bungled it up:
public class Fog extends Actor
{
    public boolean foggy;
    public int foggyTimer;
    int i;
    /**
     * Act - do whatever the Fog wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        fogTimer();
    }
    public void summonFog(){
        getWorld().addObject(this,600,getRandomNumber(110,227));
        fogRollIn();
        foggy = true;
    }
    public void fogTimer(){
        if(foggy){
            foggyTimer++;
            if(((foggyTimer+1)%getRandomNumber(600,1200)==0)){
                getWorld().removeObject(this);
                foggy = false;
            }
        }
    }
    public void fogRollIn(){
        while(i<60){
            i++;
            setLocation(getX()-10, getY());
        }
    }
    public int getRandomNumber(int start, int end){
        int normal = Greenfoot.getRandomNumber(end-start+1);
        return normal+start;
    }
}
only the summonFog() part was affected, so that should be all good. I'm sorry for the inconvenience.
danpost danpost

2020/3/17

#
How can summonFog add the Fog object into a world when getWorld does not return a world when the actor is not in a world? Also, the loop in fogRollIn will execute to completion between two consecutive frames, only visually showing the end result.
CodesForFun CodesForFun

2020/3/17

#
danpost wrote...
How can summonFog add the Fog object into a world when getWorld does not return a world when the actor is not in a world? Also, the loop in fogRollIn will execute to completion between two consecutive frames, only visually showing the end result.
Right, so I get what you're saying and made the world show the fog so that it will be there when making it roll in. So now the fog shows up. I confirmed it. However, I still get the same error of NullPointerException on the same line of code, where the FogBall calls summonFog(). I have no idea why this is happening.
danpost danpost

2020/3/17

#
Did you remove the addObject line in summonFog?
CodesForFun CodesForFun

2020/3/17

#
Yeah, I did.
CodesForFun CodesForFun

2020/3/17

#
Ok, so I narrowed the problem down. It seems to be happening whenever I try to call in summonFog() for some reason.
CodesForFun CodesForFun

2020/3/17

#
I figured it out! When I declared theFog to make it so that I could call Fog from another object, I didn't input
new Fog();
which returned an error because it was trying to call something that didn't exist!
You need to login to post a reply.