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

2012/11/20

Another java.lang.NullPointerException question

mickyg mickyg

2012/11/20

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    private int vSpeed=5;
    private int hSpeed=5;
    private Score1 score1;
    private Score2 score2;
    public Paddle paddle1;
    public Paddle2 paddle2;

    /**
     * Set speed of the ball.
     */
    public Ball(Score1 pointScore1, Score2 pointScore2, Paddle paddle1, Paddle2 paddle2)
    {
        score1 = pointScore1;
        score2 = pointScore2;
        paddle1 = paddle1;
        paddle2 = paddle2;
    }

    /** 
     * Do whatever a ball does.
     */
    public void act()
    {
        move();
        if (checkLeft() == true) {
            score1.add(1);    
            setLocation(getWorld().getWidth()/2, Greenfoot.getRandomNumber(getWorld().getHeight()));
            resetPaddle();

            Greenfoot.delay(25);
        }
        if (checkRight() == true) {
            score2.add(1);    
            setLocation(getWorld().getWidth()/2, Greenfoot.getRandomNumber(getWorld().getHeight()));
            resetPaddle();

            Greenfoot.delay(25);
        }
    }

    /**
     * Make the ball move.
     */
    public void move()
    {
        setLocation( getX() + hSpeed, getY() + vSpeed );
        if (checkTop() == true) {
            vSpeed = vSpeed * -1;
        }
        if (checkBottom() == true) {
            vSpeed = vSpeed * -1;
        }
        if (checkPaddle() == true) {
            hSpeed = hSpeed * -1;
        }
        if (checkPaddle2() == true) {
            hSpeed = hSpeed * -1;
        }
    }

    public void resetPaddle()
    {

        paddle1.setLocation(575, 200);
        paddle2.setLocation(575, 200);
    }

    /**
     * Check to see if the ball has reached the left side.
     */
    public boolean checkLeft()
    {
        int margin = getImage().getWidth()/2;
        if (getX()-margin < 0){
            return true;
        }
        return false;
    }

    /**
     * Check to see if the ball has reached the right side.
     */
    public boolean checkRight()
    {
        int margin = getImage().getWidth()/2;
        if (getX()+margin > getWorld().getWidth()){
            return true;
        }
        return false;
    }

    /**
     * Check to see if the ball has reached the top.
     */
    public boolean checkTop()
    {
        int margin = getImage().getWidth()/2;
        if (getY()-margin < 0){
            return true;
        }
        return false;
    }

    /**
     * Check to see if the ball has reached the bottom.
     */
    public boolean checkBottom()
    {
        int margin = getImage().getWidth()/2;
        if (getY()+margin > getWorld().getHeight()){
            return true;
        }
        return false;
    }

    /**
     * Check to see if the the ball has hit a paddle.
     * If true, bounce off the paddle.
     */
    public boolean checkPaddle()
    {
        int margin = getImage().getWidth()/2;
        if ( getOneObjectAtOffset(0,0,Paddle.class ) !=null) {
            return true;
        }
        return false;
    }

    /**
     * Check to see if the the ball has hit a paddle.
     * If true, bounce off the paddle.
     */
    public boolean checkPaddle2()
    {
        if ( getOneObjectAtOffset(0,0,Paddle2.class ) !=null) {
            return true;
        }
        return false;
    }
}
java.lang.NullPointerException at Ball.resetPaddle(Ball.java:74) at Ball.act(Ball.java:45) I received the prior error message. I have read through several posts regarding this error but still can't seem to figure out what to do to get rid of it. Hopefully someone help me with this. Thanks in advance.
danpost danpost

2012/11/20

#
It is evident you are creating the paddles before creating the ball, as you are bringin in the paddle objects in the constructor. What is not evident, is whether they were added to the world (in the world class); or whether they could have been removed from the world (only you would know where, if anywhere); or whether the ones you added to the world are the same ones you brought into the Ball class through its constructor.
mickyg mickyg

2012/11/20

#
Sorry, I'm assuming you are looking for code from the other classes and the world. I don't believe I removed the objects and I believe they are the same ones. Here is additional code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Paddle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Paddle extends Actor
{
    
    /**
     * Create the paddle
     */
    public Paddle()
    {
        GreenfootImage img = new GreenfootImage( 20, 100 );
        img.fillRect( 10, 10, 20, 100);
        setImage(img);
    }

    /**
     * Do whatever the Paddle wants to do.
     */
    public void act() 
    {
        keystrokes();
//         resetPaddle();
    } 
    
    /**
     * Make the paddle move up and down.
     */
    public void keystrokes()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY() -3);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY() +3);
        }
    }
    
//    public void resetPaddle()
//    {
//       setLocation(15, 200);
//     }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PongTable here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PongTable extends World
{

    /**
     * Constructor for objects of class PongTable.
     */
    public PongTable()
    {    
        super(600, 400, 1); 
        createWorld();
    }
    
    public void createWorld()
    {        
        Score1 score1 = new Score1();
        addObject(score1, 555, 15);
        Score2 score2 = new Score2();
        addObject(score2, 45, 15);
        Paddle paddle1 = new Paddle();
        addObject( new Paddle(), 575, getHeight()/2);
        Paddle2 paddle2 = new Paddle2();
        addObject( new Paddle2(), 15, getHeight()/2);
        addObject( new Ball(score1, score2, paddle1, paddle2), getWidth()/2, getHeight()/2);
    }
}
danpost danpost

2012/11/20

#
OK, I see it. It was in your Ball class. See lines 25 and 26. They do not set the values of the class variables 'paddle1' and 'paddle2'. You need to use
this.paddle1 = paddle1;
this.paddle2 = paddle2;
mickyg mickyg

2012/11/20

#
ok thank you! Reading other posts it said I needed to set the values, but I didn't know what it meant or how to do it.
You need to login to post a reply.