2014-11-08 62 views
-1

我想优化我使用教程构建的Animation类,但每当我尝试将BufferedImage添加到类型为BufferedImage的LinkedList时,我都会得到一个NullPointerException。BufferedImage 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); 
    } 
} 

为什么我不能在BufferedImage中添加到LinkedList的?

+1

你在哪里初始化'img'?在向这个LinkedList添加元素之前,你应该这样做......你如何调用这个构造函数? – vefthym 2014-11-08 09:37:00

+0

我试过 anim = new动画( 2, game.getBufferedImage(0,0), game.getBufferedImage(0,1)) – 2014-11-08 09:40:21

回答

5

您需要将图像添加到它

现在你只是声明它的指针之前创建一个新的LinkedList,所以IMG可能是像空当您尝试添加到它。

img = new LinkedList<BufferedImage>();