0

Too many times I have to use a List of an Object instead of the object, If I use

MyObj a = null;
a.setParameter = 0;

It works. But If I use

List<MyObj> a = null;
MyObj b = getObjectFromSomewhere();
a.add(b);

It fails with error : java.lang.NullPointerException?

And How I can see If my new List<MyObj> a is "null" or without any element inside?

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
Liz Lamperouge
  • 681
  • 14
  • 38
  • 2
    I'm guessing `a.setParameter` is a static field of MyObj, or else that would also throw a null pointer exception. You can assign to null just fine (it overwrites the old value with the new) but you can't call a function on a null object because it needs an instance to operate on (unless it is a static function, which bypasses referencing any instance of the class) – Tezra Aug 07 '17 at 16:37
  • https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – nasukkin Aug 07 '17 at 16:47

3 Answers3

6

A list is a container that you have to put elements into. The list has to exist prior to putting anything in it (so you can have an empty list, note).

So this:

List<MyObj> a = null;

declares a variable pointing to a null - not an actual container. Try:

List<MyObj> a = new ArrayList<>();

(note that lists come in different varieties, with different performance characteristics, and the above is just a commonly used variety chosen for illustration)

(to address your first point re a.setParameter, I guess that is a static method - defined for the class, not the instance of a class - and confusingly Java will let you reference that via an instance)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • So why Eclipse allow us to istantiate a list = null? What is the point ? – Liz Lamperouge Aug 07 '17 at 16:39
  • 2
    *Any* object reference can be a null, note, and a list is simply an object. I agree however that a null list is confusing, and would often prefer an empty list, such that I don't have to check it prior to iteration etc. – Brian Agnew Aug 07 '17 at 16:41
  • @LizLamperouge *"So why Eclipse allow us to istantiate a list = null? What is the point ?"* Neither the inventor of the `null` concept doesn't really know... https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare – Timothy Truckle Aug 07 '17 at 16:47
  • @BrianAgnew thanks for the answer, at the end I use if(a.isEmpty()) to see if The list is empty or not. Now I very understand the meaning of "null". (thanks to Timothy Truckle too) – Liz Lamperouge Aug 08 '17 at 07:18
0

Eclipse allows you to initiate it to a null because Eclipse thinks that you will be pointing an object to this reference somewhere later in your code.

so its your responsibility to have it refer to an object before using any of the List methods.

Jeeppp
  • 1,553
  • 3
  • 17
  • 39
-1

doing this List<MyObj> a = null makes no initialization in memory so a point to nothing

what you should do is

List<MyObj> a = new ArrayList<>();
a.add(getObjectFromSomewhere());

How I can see If my new List a is "null" or without any element inside?

if(a == null){...} // to check if array not initialized 
if(a.size() == 0){...} // to check if array have no element

keep in mind calling a.size() when a is null will throw NullPointerException

Ali Faris
  • 17,754
  • 10
  • 45
  • 70