• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to determine if a List allows null elements

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have read on the java docs website that "A NullPointerException is thrown is an attempt is made to store a null object and null elements are not allowed in the list"

So, how do I know upfront if null elements are allowed in the list ??


Thanks.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sense does it make to store NULL in an array list ?

If there is no value present at position 4534234 in an ArrayList, then list.get(4534234 ) will automatically return null. There is absolutely no use for a null value in a List.

It is possible to have a null key in a HashMap. And it makes sense. Look at it as a default value. So, One null key is allowed and many null values.

But that only applies for the HashMap, TreeMap for instance will choke upon feeding it with null values, since all objects need to be comparable. Null - well - is not.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lavi mendonca wrote:I have read on the java docs website that "A NullPointerException is thrown is an attempt is made to store a null object and null elements are not allowed in the list"

That quote appears to be from the List#add(E) documentation. You will have to read the documentation for implementing classes, eg AbstractListl#add(E) ArrayList#add(E) and LinkedList#add(E) and see whether they prohibit null values.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastian Janisch wrote:If there is no value present at position 4534234 in an ArrayList, then list.get(4534234 ) will automatically return null. There is absolutely no use for a null value in a List.


If an ArrayList (and any other List implementation) has less than 4534235 elements, then list.get(4534234) will throw an IndexOutOfBoundsException. It will not return null.
It will only return null if the value at position 4534234 is null - and guess what, that means that the List contains null values.

If you get an unknown List reference, there is just one way to find out - try it:
This does pose one threat - if add is allowed but remove isn't. That will leave your List with one trailing null element. I'm don't think you want that.
 
lavi mendonca
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Sebastian Janisch and Campbell Ritchie for your reply.

I checked all the implementations - ArrayList, LinkedList and Vector.. All of them permit nulls.

Does this mean that this point in time, all the available list implementations permit nulls ?


Thanks.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lavi mendonca wrote:Does this mean that this point in time, all the available list implementations permit nulls ?


I have a question for you first: can you name all the available List implementations?

The answer is no. Yes, all implementations that are mentioned on the API page of List itself allow null values. Yes, most internal classes like those returned by Arrays.asList, Collections.unmodifiableList and Collections.synchronizedList allow null values. However, the List returned by Collections.checkedList does not allow null values - all elements that will be added must be an instance of the provided class. The bad thing is that trying to add a null element will not throw a ClassCastException like the API suggests but a NullPointerException instead.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lavi mendonca wrote:Thank you Sebastian Janisch and Campbell Ritchie for your reply.

You're welcome

Does this mean that this point in time, all the available list implementations permit nulls ?


Thanks.

Rob has already answered that question. You will have seen that ArrayList and LinkedList permit nulls.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic