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

2019/5/13

Null Pointer Exception in Breakout

SpicySpaghetti19 SpicySpaghetti19

2019/5/13

#
So I recently started coding breakout but I can't seem to get rid of this null pointer exception. I know that it has something to do with the bricks not being in the world and then being called but i'm not sure how to go about fixing that. Heres my Code
import greenfoot.*;

/**
 * The ball of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author mik
 * @version 1.0
 */
public class Ball extends Actor
{
    private int deltaX;         // x movement speed
    private int deltaY;         // y movement speed
    private int count = 2;
    private int score = 0;
    private boolean stuck = true;   // stuck to paddle
    private Counter counting; 
    public Ball(Counter c)
    {
        counting = c;
    }
    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck && this != null) 
        {
            move();
            makeSmoke();
            checkOut();
            checkBlock();
        }
    }

    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
    }

    /**
     * Check whether we've hit one of the three walls. Reverse direction if necessary.
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1)
        {
            deltaX = -deltaX;
        }
        if (getY() == 0) 
        {
            deltaY = -deltaY;
            
        }
    }

    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            ((backdrop) getWorld()).ballIsOut();
            getWorld().removeObject(this);
        }
    }

    /**
     *  Check if the ball touches the block
     */
    private void checkBlock()
    {  
        Actor block = getOneIntersectingObject(Brick.class);
        
        if (block != null)
        {
            getWorld().removeObject(block);
            deltaY = -deltaY;
        }
    }
    

    private void checkPaddle()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        if (paddle != null) {
            deltaY = -deltaY;
            int offset = getX() - paddle.getX();
            deltaX = deltaX + (offset/10);
            if (deltaX > 7) {
                deltaX = 7;
            }
            if (deltaX < -7) {
                deltaX = -7;
            }
        }            
    }

    /**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }

    /**
     * Put out a puff of smoke (only on every second call).
     */
    private void makeSmoke()
    {
        count--;
        if (count == 0) {
            getWorld().addObject ( new Smoke(), getX(), getY());
            count = 2;
        }
    }

    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5;
        deltaY = -5;
        stuck = false;
    }
}
This is my main world code also, just incase you needed that
import greenfoot.*; 

/**
 * The game board. The board had a paddle that can move.
 * 
 * @author mik
 * @version 1.0
 */
public class backdrop extends World
{
    private Paddle paddle;
    
    /**
     * Constructor for objects of class Board.
     * 
     */
    public backdrop()
    {    
        super(460, 520, 1);
        setPaintOrder ( Counter.class, Ball.class, Smoke.class );
        
        
        //ADD OBJECTS
        Counter count = new Counter();
        addObject(count, 50, 500);
        paddle = new Paddle(count);
        addObject ( paddle, getWidth() / 2, getHeight() - 40 );
              
        //ADDING BRICKS TO WORLD 
        for (int i = 1; i < 5; i++)  
        {
            for(int j = 1; j < 6; j++) 
            {
                addObject(new Brick(),90 * i , 60 * j);
            }
        }
        
        
    }
    
    public void ballIsOut()
    {
        paddle.newBall();
    }
    
}
Super_Hippo Super_Hippo

2019/5/13

#
Swap lines 30 and 31 of the Ball class. ("this" will never be null, you can remove that condition in line 26.)
SpicySpaghetti19 SpicySpaghetti19

2019/5/15

#
Thanks :)
You need to login to post a reply.