user1315906
user1315906

Reputation: 3494

How to handle the exception when a HashMap returns NULL

I have a method as follows; It does some computation and then returns a HashMap.

public Map<Integer, Animal> method() {
     // do some computation
     returns hashMap;
    }

1.) What hapence if the hash map returns null, will i get an null pointer exception ? and if so should i handle it as

public Map<Integer, Animal> method() throws{

So that the calling method will handle it ?

Upvotes: 1

Views: 19040

Answers (3)

Brian
Brian

Reputation: 17309

A NullPointerException occurs when you try to use the . on a null variable. For example:

String s = null;
char c = s.charAt(0); // Tries to access null, throws NPE

Another place a NullPointerException can occur is when you try to unbox a null wrapper:

Integer integer = null;
int i = integer; // Tries to unbox null, throws NPE

These are the only ways you can ever get a NullPointerException. Well, unless someone explicitly throws one:

throw new NullPointerException();

But you should never do that. Throw an IllegalArgumentException instead.

That being said, returning null from a method won't produce a NullPointerException. However, using the . on the result of that method when it does return null can:

Map<Integer, Animal> map = method();
map.get(20); // Throws NPE if method() returns null

To fix this, you use a null-check:

Map<Integer, Animal> map = method();
if (map != null)
    map.get(20); // Never throws NPE

Note that we can still use map as a reference, but we can't access it since it's null. That's the distinction it seems you're missing.

Edit for your comment

So you are suggesting that I should leave method() as it is (without exception handling) and check it from the calling function?

That's one possible option. Another is throw an exception to indicate that it's null if it's not supposed to be.

The big question is, is it allowed to be null? If it is, then leave method() as is, and check if it's null when you call it like I have above. Indicate in your Javadoc comments that this method may return null.

If it isn't allowed to return null, then when the method is called, either create the Map or throw an exception. You can even create the map only when it's null (this is called lazy initialization):

public Map<Integer, Animal> method() {
    if (hashMap == null)
        hashMap = new HashMap<Integer, Animal>();
    return hashMap;
}

If your hashMap is being created somewhere else, so this method shouldn't be called until after that other place created it (called a precondition), then you should throw an exception. For precondition violations, I usually use IllegalStateException:

public Map<Integer, Animal> method() {
    if (hashMap == null)
        throw new IllegalStateException("Must call otherMethod() to initialize hashMap before calling this method.");
    return hashMap;
}

Upvotes: 1

Addict
Addict

Reputation: 823

No, it will not give null pointer exception because you are just returning HashMap. In calling method before doing any operation on returned HashMap make sure to check that returned value is not null.

Upvotes: 1

Polyana Fontes
Polyana Fontes

Reputation: 3216

When you get a object check if it is null... If a null value is not intended you can specify that it may throw a NullPointerException just for documentation since it's a RunTimeException and all RuntimeExceptions can be trown anywhere.

You can also do a:

try{
    /* calculate */
}
catch(NullPointerException e){
    /* process exception */
}

to process when a NullPointerException happens, so you can revert the actions or close the resources or throw a more descriptive exception...

For example:

try{
    Type o = (Type) map.get(key);
    o.doSomething();
}
catch(NullPointerException e)
{
    throw new RuntimeException("Null values are not accepted!", e);
}

Upvotes: 2

Related Questions