关于ArrayList的NullPointerException?

时间:2016-04-09 10:02:22

标签: java list exception arraylist nullpointerexception

java.lang.NullPointerException
    at videostore.BinaryFile.adderoo(BinaryFile.java:47)
    at videostore.VideoStore.jButton1ActionPerformed(VideoStore.java:384)
    at videostore.VideoStore.access$100(VideoStore.java:17)
    at videostore.VideoStore$2.actionPerformed(VideoStore.java:189)

第47行:if (videosList == null || videosList.size() == 0)

第384行:BinaryFile.adderoo(vid);

其中涉及以下方法:

public void adderoo(Video v) {
        if (videosList == null || videosList.size() == 0) {
            videosList = new ArrayList<>(10);
        }
        videosList.add(v);
    }

第17行:public class VideoStore extends javax.swing.JFrame {

第189行:jButton1ActionPerformed(evt);

基本上我试图将一个Video对象添加到我创建的名为 videosList 的Arraylist中。但是每次我尝试时都会遇到异常,我不知道为什么,因为我用一个大小初始化了ArrayList ..

初始化我的BinaryFile类中的arraylist:public static ArrayList<Video> videosList;

然后我有一个名为 load 的方法,我在我的主 videoStore 类中运行它的构造函数:

public void load() throws Exception {

        BufferedReader br = new BufferedReader(new FileReader(FILE_NAME));
        if (br.readLine() != null) {
            try {
                File log = new File(FILE_NAME);
                FileInputStream fileIn = new FileInputStream(log);
                ObjectInputStream in = new ObjectInputStream(fileIn);
                ArrayList<Video> videosList = (ArrayList)in.readObject();
                in.close();
                fileIn.close();
            } catch (Exception i) {
                i.printStackTrace();
            }
        } else {
            ArrayList<Video> videosList = new ArrayList<>(10);
        }
        br.close();
    }

此方法的目的是检查文件是否已包含ArrayList,如果它包含它,则将其反序列化并将其放入ArrayList中。

我觉得我已经覆盖了所有的停留点,所以我很难知道为什么会发生异常。

0 个答案:

没有答案