This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dalvengyr
Dalvengyr wrote ...

2014/11/19

List add object exception?

Dalvengyr Dalvengyr

2014/11/19

#
I have a list. And I have declared it:
1
private static List<Unit> AliveGroup;
Then I tried to add an object to that list:
1
2
if (AliveGroup == null || !AliveGroup.contains(this))
    AliveGroup.add(this);
I'm not sure I'm doing it right but it always throws an exception no matter how. It was something like "Null pointer exception". Guess the List is still null, I don't have any idea how to instantiate it :/ Please help! Thanks anyway. EDIT: But this one doesn't throw error:
1
2
if (AliveGroup == null || !AliveGroup.contains(this))
    //AliveGroup.add(this);
So I guess I need to do something before I add the object to the list but what? That action is inside Unit class btw.
Dalvengyr Dalvengyr

2014/11/19

#
Please, help me guys
Dalvengyr Dalvengyr

2014/11/19

#
Helooo... Anybody home?
danpost danpost

2014/11/19

#
This line:
1
private static List<Unit> AliveGroup;
is equivalent to this line:
1
private static List<Unit> AliveGroup = null;
Declaring an object reference does not give it an object to reference. You must create and assign one to the field. The List class is an interface and cannot be directly instantiated; however, there are multiple classes offered in the java package that implement it with different behaviors. The most commonly used one is probably the ArrayList class, which creates a basic list. The following should work:
1
private static List<Unit> AliveGroup = new ArrayList<Unit>();
You will need to import 'java.util.ArrayList' as well as 'java.util.List'.
davmac davmac

2014/11/20

#
The code that you have to add an object into a list:
1
2
if (AliveGroup == null || !AliveGroup.contains(this)) 
    AliveGroup.add(this);
... implies a misunderstanding about how lists and objects in general work. A List is an object like any other. If you have a List variable, and it has the value 'null', then it does not refer to a list. If it does not refer to a list, you cannot call the 'add' method (or any other method) on it.
I'm not sure I'm doing it right but it always throws an exception no matter how. It was something like "Null pointer exception". Guess the List is still null (...)
Indeed the list is still null, because you specifically allow that in the 'if' statement. If you take the second part of the condition out from your if statement you have effectively got this:
1
2
if (AliveGroup == null
    AliveGroup.add(this);
This could never work. It effectively says: "if I have a null reference, call a method on that null reference". You can never call a method on a null reference, for a list or for any other type. Danpost has shown you how to properly instantiate a list in his post, but I wanted to elaborate on the issues in your code because you do seem quite confused. I hope this helped.
Dalvengyr Dalvengyr

2014/11/21

#
Thanks guys for your help, it works correctly now :)
You need to login to post a reply.