Question

I'm currently going through Eric Robert's Art and Science of Java, which uses the ACM Java libraries. One of the exercises has the student build a clone of Breakout. I'm having issues with the animation of objects, so please have a look at this code and if possible tell me why the ball isn't moving.

This is code sample I wrote to isolate the behavior that's giving me troubles, but after spending the entire evening on it, I thought I better ask for help since I wasn't making any progress at all. What the code is supposed to achieve is merely to move the object. (Note: this is NOT a homework problem.)

I set up the canvas with the ball in setup(), and intend to make the ball move in play() but nothing happens.

The code has also been pasted to: http://pastebin.com/vy3rMrZw

   package codeSamples_II;

import acm.program.*;
import acm.graphics.*;


public class PlayBall extends GraphicsProgram {

private static final int DELAY = 50;
private GOval ball;
private static final int BALL_RADIUS = 10;

public void run() {
    setup();
    play();
}

private void setup() {
    GOval ball = new GOval(0,0, BALL_RADIUS*2, BALL_RADIUS*2);
    ball.setFilled(true);
    add(ball);
}

public void play() {
    while (ball.getY() < 200) {
        ball.move(5, 5);
        pause(DELAY);
    }
}

}

Thanks a lot!

Était-ce utile?

La solution

Oh, duh! I can't believe I didn't see this the first time...

Here is what is happening.

When your PlayBall class in instantiated, your define private GOval ball. This creates an null pointer (a variable that has no value) to a GOval object.

In setup(), you create a new GOval variable called ball. This one is different from the one you created globally. This new variable named ball is a local variable, and only exists in the setup() method. It is not the same as the global variable ball. So in play() when you try and move ball, you are trying to move the global variable ball, which is not the same as the local variable you created in setup(). In fact, the global variable ball was never instantiated (never created, or null), so you can't actually operate on it. This is why you are getting the null pointer error.

To fix your code, you need to ensure that the global variable is being assigned to the new ball, not a local variable. It is easy enough to fix, you just need to remove the GOval identifier before ball = new Goval( ... ). Your code will look like this:

package codeSamples_II;

import acm.program.*;
import acm.graphics.*;


public class PlayBall extends GraphicsProgram {

private static final int DELAY = 50;
private GOval ball;
private static final int BALL_RADIUS = 10;

public void run() {
    setup();
    play();
}

private void setup() {
    ball = new GOval(0,0, BALL_RADIUS*2, BALL_RADIUS*2);
    ball.setFilled(true);
    add(ball);
}

public void play() {
    while (ball.getY() < 200) {
        ball.move(5, 5);
        pause(DELAY);
    }
}
}

You may want to read up on variable scoping to get a better idea of what is going on if you still don't understand.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top