2014-11-08 47 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>();