0
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)

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

Line 384:BinaryFile.adderoo(vid);

Which refers to the following method:

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

line 17: public class VideoStore extends javax.swing.JFrame {

Line 189: jButton1ActionPerformed(evt);

Essentially I'm trying to add a Video object to an Arraylist I have created named videosList. However every time I try, I get that Exception and I'm not sure why, because I initialized the ArrayList with a size..

initializing the arraylist in my BinaryFile class: public static ArrayList<Video> videosList;

Then I have a method named load, which I run in my main videoStore class in it's constructor:

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();
    }

The intention of this method is to check if a file contains an ArrayList already, and if it contains it, it de-serializes it and places it into the ArrayList.

I feel like I've covered all the stops, so I'm stumped as to why the exception is occurring.

blue sky
  • 161
  • 1
  • 13
  • 1
    @KevinEsche thanks! that fixed it right up. i appreciate it :) post as an answer so i can give you the green check thing? – blue sky Apr 09 '16 at 10:06
  • 2
    If videoList is `null` the second part of the if condition shouldn't be evaluated thanks to lazy evaluation. You should try with a different and safer check. For instance you could check `if videosValues != null && videosList.size() > 0` – fredmaggiowski Apr 09 '16 at 10:10

2 Answers2

1

Your if tests the first condition, if videosList is null, thanks to Short-Circuit Evaluation it shouldn't test the second condition videosList.size() == 0

if (videosList == null || videosList.size() == 0)

I suggest you to use safer way to test if your ArrayList is null or empty using, for instance, provided method .isEmpty() or a different if condition, like the one i posted in my comment:

if (videosList != null && !videosList.isEmpty())

Note: in the comment I used videosList.size > 0.. but since we have the .isEmpty() it's better use it.

fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
  • why is negating the condition a _safer way_? – P.J.Meisch Apr 09 '16 at 10:32
  • I've always worked with negating conditions as a best practice. And to me (might be an opinion but it helped me a lot and I've been taught this way) it's actually a safer way to organise your codebase – fredmaggiowski Apr 09 '16 at 10:43
0

As per the condition which you have given, the evaluation of videosList.size() can throw NullPointerException if the list itself is null.

You should change the condition to something like this:

!(videosList != null && videosList.size() > 0)

Here is the code snippet:

public void adderoo(Video v) {
    if (!(videosList != null && videosList.size() > 0)) {
        videosList = new ArrayList<>(10);
    }
    videosList.add(v);
}
user2004685
  • 9,548
  • 5
  • 37
  • 54