Dun*_*mer 1 java nullpointerexception
我正在制作一个游戏并且它给了我一个NullPointerException我认为意味着变量或者我想要做的是不返回值?我正在使用以下代码:
package OurGame;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Coin extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Image img;
int x,y;
Timer t;
Random r;
public Coin() {
x = 50;
y = 50;
System.out.println("New coin created: " + x + ", " +y);
ImageIcon i = new ImageIcon("C:/coin.png");
img = i.getImage();
System.out.println(i);
t = new Timer(3000,this);
t.start();
}
@Override
public void actionPerformed(ActionEvent arg0) {
move();
}
public void move() {
setX(r.nextInt(640));
setY(r.nextInt(480));
}
public void setX(int xs) {
x = xs;
}
public void setY(int ys) {
y = ys;
}
public Image getImage(){
return img;
}
public int getX(){
return x;
}
public int getY() {
return y;
}
}
Run Code Online (Sandbox Code Playgroud)
错误发生在这里:
setX(r.nextInt(640));
Run Code Online (Sandbox Code Playgroud)
完整的错误输出如下:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at OurGame.Coin.move(Coin.java:46)
at OurGame.Coin.actionPerformed(Coin.java:40)
at javax.swing.Timer.fireActionPerformed(Unknown Source)
at javax.swing.Timer$DoPostEvent.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
我没有看到任何这种情况发生的原因,但也许你可以提供帮助.
谢谢.
| 归档时间: |
|
| 查看次数: |
744 次 |
| 最近记录: |