我正在制作一个简单的程序,用户必须避开一些障碍(来自抽象的障碍类),如岩石,水母和游泳者(所有扩展障碍类)。我在我创建的构造函数Background中创建了其中的三个对象,但是,我在这些对象上遇到了NullPointerException。任何人都可以告诉我,为了得到这个例外,我做错了吗?
Background对象是在Runner类中创建的,但我只是在构造函数中得到了异常。
public class Background extends JPanel implements Runnable
{
public static Dimension d;
public Thread game;
public int hlth;
private ArrayList<Obstacle> os;
private User u;
private JLabel pic;
private ImageIcon picture;
private Tools t;
public Background(Dimension dim)
{
//set up the JPanel dimension
d = dim;
this.setPreferredSize(d);
//unrelated - just set up the background in a jlabel
//unrelated - just set up a health jlabel at top of screen
//set up the arraylist of obstacles
os = new ArrayList<Obstacle>();
os.add(new JellyFish(t.rX(), t.rY())); //the program is crashing here
os.add(new Rock(t.rX(), t.rY()));
os.add(new Swimmer(t.rX(), t.rY()));
u = new User();
game = new Thread(this);
}
//not related
}
public class JellyFish extends Obstacle
{
public JellyFish()
{
super();
}
public JellyFish(int useX, int useY)
{
super(useX, useY);
}
//unrelated - draw the JellyFish using graphics
}
提前致谢!