Java BuffereImage的链接列表
我正在尝试优化使用教程构建的动画类,但每当我尝试将BuffereImage添加到BuffereImage类型的LinkedList时,就会出现NullPointerException 这是我的代码:Java BuffereImage的链接列表,java,animation,linked-list,bufferedimage,Java,Animation,Linked List,Bufferedimage,我正在尝试优化使用教程构建的动画类,但每当我尝试将BuffereImage添加到BuffereImage类型的LinkedList时,就会出现NullPointerException 这是我的代码: public class Animation { private int speed; private int frames; private int index = 0; private int count = 0; private LinkedList&
public class Animation
{
private int speed;
private int frames;
private int index = 0;
private int count = 0;
private LinkedList<BufferedImage> img;
private BufferedImage currentImg;
public Animation(int speed, BufferedImage img1, BufferedImage img2)
{
this.speed = speed;
img.add(img1);
img.add(img2);
}
}
为什么我不能将BuffereImage添加到LinkedList?在将图像添加到LinkedList之前,您需要创建一个新的LinkedList 现在您只是声明了一个指向它的指针,所以当您尝试添加到img时,img可能类似于null
img=新链接列表 在哪里初始化img?您应该在将元素添加到此LinkedList之前执行此操作。。。你如何调用这个构造函数?我已经尝试过这个anim=newanimation2,game.getBufferedImage0,0,game.getBufferedImage0,1