BufferedImage的LinkedList

Tud*_*oiu -1 java animation bufferedimage linked-list

我正在尝试优化我使用教程构建的Animation类,但每当我尝试将BufferedImage添加到BufferedImage类型的LinkedList时,我都会得到NullPointerException.

这是我的代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能将BufferedImage添加到LinkedList?

Eri*_*ric 5

在向其添加图像之前,您需要创建一个新的LinkedList

现在你只是声明一个指向它的指针,所以当你尝试添加它时,img可能就像null一样.

img = new LinkedList<BufferedImage>();