Saturday, 11 May 2013

Handling null in a Collection.

One of the general problem programmers land into is the NullPointerException. This post will mainly focus on how do we handle this null value. Specially in case of Collections what is the difference between and empty collection, a null Collection and a Collection containing a null value.

   Before we see how to handle null value remember that member(instance) variables are assigned default values if they are not initialized where as for local variable you need to explicitly initialize them before using them.So when we define a reference type as an instance variable it is by default set to null.Complete set of default values assigned to  instance variables are as follows -

Good Practice

Important point to note while comparing String literals is always put the literal to the L.H.S(Left hand side) of the == or equals() operator. For example -
        String typeOfOS;
        if("linux".equalsIgnoreCase(typeOfOS))
        {
            System.out.println("Very good choise of OS");
        }

Why so you might ask? Reason is simple if your variable typeOfOS is null it will throw a java.lang.NullPointerException.

Handling null in Collections

    It is important you understand the difference between a reference being null, Collection being empty and Collection containing null as an element.

  1. Reference being null

    List<String> operatingSystems = null;
    if(null == operatingSystems)
    {
        System.out.println("operatingSystems reference has not been initialized yet");
    }


    Explanation : No memory is allocated on heap in this case. This is just a reference that we have declared. We can check it for null as shown above.
  2. Collection being empty

            List<String> operatingSystem = new ArrayList<String>();
            if(operatingSystem.isEmpty())
            {
                System.out.println("List is empty");
            }


    Explanation : Here we create an object of List. Memory is allocated on the heap but there is no element in the List i.e the List is empty.You can check if Collection is empty or not by using .isEmpty() function.
  3. Collections containing null as an element


    Ground Work :
    We know List can have duplicate elements. This means a list can have multiple null values stored in it. Also in a Set duplicates are not allowed and hence only one null value is allowed. Never the less null is an acceptable value as an element in Collection. There are exceptions like EnumSet where null is not allowed but thats a rare usage.

            List<String> operatingSystem = new ArrayList<String>();
            operatingSystem.add(null);
            System.out.println("List is " + operatingSystem);
            if(operatingSystem.isEmpty())
            {
                System.out.println("List is empty");
            }
            else
            {
                System.out.println("List is not empty");
            }


    Output :



    Explanation : List is clearly not empty as we have added an element(null) to it. Whole point being null can be an element stores in a Collection.If you wish to remove null from the List you can simple say .remove(null).
t> UA-39527780-1 back to top