Return empty arrays or collections, not nulls

Posted on 2011-04-26
Last Modified: 2012-06-21
Return empty arrays or collections, not nulls

I was reading above staemtent. I have not understood the statement, concept behind clearly.
Please advise. Any ideas, sugestions, sample code, links, resources highly appreciated. thanks in advance
Question by:gudii9
    LVL 23

    Assisted Solution

    Obviously accessing members of null references will cause NullPointerException.
    Hence to overcome from that, the statement suggests to return empty arrays or collections.
    LVL 47

    Assisted Solution


    I'd think that this is because it is easier to use
    empty collections for the calling program
    If people foget to check for null -
    null will trhrow exception, wheraes apmpty collection
    will usually work in all codes - just traversing through nothing.
    So if folks are expecting to get ArrayList from method - let them
    have ArrayList wuith no elements rather
    than returning null
    LVL 26

    Accepted Solution

    What this point says is it's better to do this:

    public List<String> getNamesOfMyFriends() {
         if (haveNoFriends)
             return new ArrayList<String>() ;
             ... return the names of the friends

    than to do this:

    public List<String> getNamesOfMyFriends() {
         if (haveNoFriends)
             return null ;       <-- Notice the difference
             ... return the names of the friends

    Both compile and work, so why is the first better?

    It's because the caller can then write code like this:

    List<String> names = getNamesOfMyFriends() ;
    int numberOfFriends = names.size() ;

    With the first version, this code always works.
    With the second version, if the user has no friends, it will throw a null pointer exception here:
    int numberOfFriends = names.size() ;    <-- names is null

    So instead you have to write:
    List<String> names = getNamesOfMyFriends() ;
    int numberOfFriends = 0 ;
    if (names != null) {
         numberOfFriends = names.size() ;

    which is a lot more code, harder to read and more likely to be wrong.

    Hope that explains it.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now