Eclipse建议使用Null Pointer异常,但我相信我初始化了我的对象 [英] Eclipse suggests Null Pointer exception, though I believe I initialized my object
问题描述
while(getWidth()> bowl.getX()+ 10){
如果我删除它,它显示它发生在下一行:
bowl.move(10.0,0);
我得出结论,eclipse没有看到我的碗初始化。
为什么? 新GOVE不是处理吗?
我在这里的一个线程中看到,一个解决方案是将声明和初始化分解为不同的行,但我认为这不太可能是一个主要的解决方案(此外,它在我的情况下没有帮助)关于这段代码的任何建议?
此代码应该创建一个圆圈,将其放在屏幕的左上角,然后按鼠标左键移动圆圈被点击。圈子成功绘制,点击后会显示NPE讯息。
import acm.program。*;
import acm.graphics。*;
import java.awt.event。*;
public class animation extends GraphicsProgram {
public void init(){
GOval bowl = new GOval(10,10);
加(碗);
addMouseListeners();
}
public void mouseClicked(MouseEvent e){
while(getWidth()> bowl.getX()+ 10){
碗.move(10.0,0);
pause(50);
}
}
私人GOval碗;
}
行:
GOval bowl = new GOval(10,10);
正在声明一个新的GOval并隐藏底部定义的全局GOval。
该行应该是:
bowl = new GOval(10,10);
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;
}
The line:
GOval bowl = new GOval(10,10);
is declaring a new GOval and hiding the global GOval defined at the bottom.
That line should just be:
bowl = new GOval(10,10);
这篇关于Eclipse建议使用Null Pointer异常,但我相信我初始化了我的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!