1

I am attempting to add 2 sub-class objects to a static Collection of super-class objects. However, when I try to add the objects to the Collection, I'm receiving a null pointer exception and I'm trying to figure out why.

public class MoverLogic{
    static Collection<Super> superCollection;

    public static void main(String[] args) {
    SubAlpha sub1 = new SubAlpha();
    SubBeta sub2 = new SubBeta();
    superCollection.add(sub1); //I'm getting the null pointer exception here
    superCollection.add(sub2);
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
dock_side_tough
  • 355
  • 2
  • 5
  • 11
  • 3
    You need to initialize `superCollection` first. – Rohit Jain Oct 21 '13 at 16:48
  • 1
    You should really **read** the stacktrace of the exception. – Luiggi Mendoza Oct 21 '13 at 16:52
  • How is it that questions like this come up every day? Either teachers are so bad, or it is for some reasons hard to understand that references are initialized with `null`. Anyway, it shows that it is counter-intuitive to have stated "superCollection is a Collection" and then to find that it is not. Remedy would be if java would demand explicit initialization of all variables. – Ingo Oct 21 '13 at 17:03
  • @Ingo Sorry mate, I never had any teachers. – dock_side_tough Oct 21 '13 at 17:09
  • So, @TomFang, do you think it would have helped you if the compiler would have said something like: "error on line 2: please iniialize variable superCollection!" – Ingo Oct 21 '13 at 17:15
  • @TomFang So pick one of the answers as answering your question and move on. Now you know. A reference has nothing in it unless you put something in it. – Radiodef Oct 21 '13 at 17:18

4 Answers4

3

Members of a class are initialized to default values when a class is constructed (or loaded statically as in your case)--like ints to 0, booleans to false, etc. Similarly, objects are initialized to null.

So in your case, superCollection is initialized to null, and when you use it, you get a NullPointerException. It is common to think collections are initialized to empty collection objects, but that isn't the case. Besides, which Collection do you mean? ArrayList? LinkedList?

So something like

static Collection<Super> superCollection = new ArrayList<>(); //Java 7

would work. Or with any other Collection you prefer.

Vidya
  • 29,932
  • 7
  • 42
  • 70
1

You will have to initialize superCollection before using it.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
JoeC
  • 1,850
  • 14
  • 12
0

You have not initialized superCollection. Before anything you have in your main, write:

superCollection = new ArrayList<Super>();

If you aren't going to change the actual collection reference, it might be beneficial of creating it directly in the declaration and adding the final modifier:

static final Collection<Super> superCollection = new ArrayList<Super>();

You will still be allowed to add or remove elements, but you can't re-assign it to anything else.

NOTE: You might change ArrayList to a different concrete impelementation of Collection if it doesn't suit your needs.

Darkhogg
  • 13,707
  • 4
  • 22
  • 24
0

You need to create your collection object eg.

 static List<Super> superCollection=new LinkedList<Super>();
Antoniossss
  • 31,590
  • 6
  • 57
  • 99