As the title says I have NPE error. It happens on line:
while (getWidth() > bowl.getX()+10) {
If I remove it, it shows it happens on next line:
bowl.move(10.0, 0);
I concluded that eclipse does not see my "bowl" initialized.
Why? Doesn't "new GOval" deal with that?
I've seen in one of the threads here that a solution was to split declaration and initialization to different lines, but I think it is unlikely to be a primary solution (besides, it didn't help in my case) Any suggestions on this code?
This code is supposed to create a circle, put it in the left-upper corner of the screen, and move the circle after mouse-button is clicked. The circle is drawn successfully, but the NPE message shows up after the click.
import acm.program.*;
import acm.graphics.*;
import java.awt.event.*;
public class animation extends GraphicsProgram {
public void init() {
GOval bowl = new GOval(10,10);
add(bowl);
addMouseListeners();
}
public void mouseClicked(MouseEvent e) {
while (getWidth() > bowl.getX()+10) {
bowl.move(10.0, 0);
pause(50);
}
}
private GOval bowl;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…