Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does Java Collections throw a NullPointerException while using Collections.addAll()

Under what condition does the Collections.addAll() method throw a NullPointerException? The official docs mention:

NullPointerException - if the specified collection contains a null element and this collection does not permit null elements, or if the specified collection is null

How do I make sure that this "Collection does not permit null elements"

public class CollectionImpl {

public void checkList(){

    List<String> var1 = new ArrayList<String>();
     var1.add("One");
     var1.add("Two");
     var1.add("Three");

     System.out.println("var1 : " + var1);

     try{
         if(Collections.addAll(var1,"Four" , "Five" , null , "1")){
            System.out.println("True"); 
         }
     }
     catch(NullPointerException e){
         System.out.println("Caught Null Pointer Exception" + e);
     }
     catch(IllegalArgumentException e){
         System.out.println("Caught IllegalArgument Exception" + e);
     }
     finally{
         System.out.println("var1 : " + var1);
     }
}

OUTPUT

var1 : [1, 2, null]
True
var1 : [1, 2, null, 4, 5, null, 6]
like image 212
technazi Avatar asked Jan 06 '16 14:01

technazi


People also ask

In which case the NullPointerException will be thrown?

Class NullPointerException Thrown when an application attempts to use null in a case where an object is required. These include: Calling the instance method of a null object. Accessing or modifying the field of a null object.

When should we throw a NullPointerException?

NullPointerException is thrown when program attempts to use an object reference that has the null value. These can be: Invoking a method from a null object. Accessing or modifying a null object's field.

What is collections addAll in Java?

The addAll() method of java. util. Collections class is used to add all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.

Why do we get NullPointerException?

What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.


2 Answers

When does Java Collections throw a NullPointerException while using Collections.addAll()

The Collections.addAll documentation tells us:

NullPointerException - if elements contains one or more null values and c does not permit null elements, or if c or elements are null

Answering your second question:

How do I make sure that this "Collection does not permit null elements"

By referring to the documentation for the class you're using. In this case (ArrayList), it's the second sentence:

Implements all optional list operations, and permits all elements, including null.

(My emphasis.)

If you don't know what class you're dealing with (you just receive a List from other code and don't/can't know what kind of list it may be), you need to document in the method accepting it whether it needs to support null (since List doesn't have something that tells you; other than trying to add one and catching the exception if it doesn't).

like image 180
T.J. Crowder Avatar answered Nov 14 '22 23:11

T.J. Crowder


Whether a collection supports null elements is a property of the collection implementation itself. It's not a setting that can be changed on a particular instance of a collection. As others have mentioned, whether a collection permits null should always be documented in the class's specification.

Your example uses ArrayList, which permits null elements, so calling addAll() with null values will always successfully add them to the list.

An example of a Collection that doesn't permit null values is ArrayDeque. If you change your declaration of var1 as follows:

Collection<String> var1 = new ArrayDeque<String>();

then you'll get a NullPointerException thrown from the call to addAll().

like image 45
Stuart Marks Avatar answered Nov 14 '22 23:11

Stuart Marks