Share

FindBugs

Tracker: Bugs

5 FalseWarning for null dereferenced parameter - ID: 1619443
Last Update: Comment added ( combat_wombat )

the bug that reports:
Method call passes null for unconditionally dereferenced parameter

gives a false warning on

Hashmap myMap = new Hashmap();
myMap.put(null, "myString");

but it is valid to have a key as null.


combat_wombat ( combat_wombat ) - 2006-12-20 13:52

5

Closed

None

Nobody/Anonymous

false positive

None

Public


Comments ( 8 )

Date: 2007-03-01 11:35
Sender: combat_wombat



the 'map' in my code was definately not null (it was a HashMap)
I couldn't condense the problem to be able to send to you (I've 1000's of
classes)
I was running FB 1.1.3.

Your example looks as if it is using all the relavant commands my class
was using.
Odd that it doesn't replicate it

How interested are you in this bug?
I could possibly try to get a small class to replicate it - but it takes
too long to run FB, so I'm not keen on doing lots of runs to see if I can
replicate it.

I have since changed my code and the line was complained about no longer
exists so I no longer see this problem in FB.
There was no problem in my code, it might have been coded a bit oddly, but
it worked fine. The method complained about was heavily used, and never
threw a NullPointer (nor would I expect it too)
So there is a problem here in 1.1.3 at least.

I must also add that the speed up of 1.2.0 is fantastic - I used to be
able to make a cup of coffee and drink it by the time it finished, but now
I only have to wait till the kettles boiled :o)



Date: 2007-03-01 01:50
Sender: ndjc


I am also seeing this problem. It seems that FindBugs only reports this
when the program is quite large, and perhaps it cannot do as complete a
data flow analysis as it might on a smaller program? The detailed warning
message lists the concrete implementations of Map, noting that a null key
is valid for HashMap and many other implementations, but is not valid for
Hashtable. In my example, the Map in question is passed in as a parameter
to the method, and is a HashMap.

I cannot send the entire source of the application to demonstrate this
problem, and if I cut the program down to a small sample, the problem is no
longer reported!


Date: 2007-02-16 02:00
Sender: dhakim


In your example- what is map? On the line "innerMap.put( null, map);",
map isn't null is it? If not, can you please post the entire function or
even classfile here, as we seem to be unable to duplicate this issue. Also
the version of findbugs you're running would be helpful, we may simply have
already corrected the issue.


Date: 2007-01-17 21:55
Sender: ksteph


Here is the actual class I am using to try and duplicate your bug. After
running FindBugs I still don't get any warnings with this code.

import java.util.HashMap;
import java.util.Map;

public class FalsePosNullDeRef {

public static void main(String[] args) {

HashMap myMap = new HashMap();
myMap.put(null, "myString");

HashMap<Object, String> tempMap = new HashMap<Object, String>();
tempMap.put(null, "something");

Map mapofMaps = new HashMap();

Object key1 = null;

Map innerMap = (Map)mapofMaps.get(key1);
if(innerMap == null)
{
innerMap = new HashMap(8);
mapofMaps.put(key1, innerMap);
}
Map map = new HashMap();
innerMap.put(null, map);

}
}



Date: 2007-01-16 18:53
Sender: ksteph


Using both code examples I can't seem to replicate this bug.


Date: 2007-01-16 18:52
Sender: ksteph


Using both code examples I can't seem to replicate this bug.


Date: 2006-12-20 17:18
Sender: combat_wombat


this is the bit of code that is being picked up by Findbugs

In other parts of the code I'm using the place that can contain the null
key to compare Object references
ie I only do == on the keys. Although I also ask the Map .contains(...)
and .get(...) which ... may be null or not

(not sure if this is confusing or not...)


Map mapofMaps = new HashMap();
//mapofMaps is made long before all this is called

Object key1 = null;
//key1 is one of my Objects



Map innerMap = (Map)mapofMaps.get(key1);
if(innerMap == null)
{//if map doesn't exist - create it
innerMap = new HashMap(8);
mapofMaps.put(key1, innerMap);
}
//set 2'nd Key as null - for my own reasons (in other places it isn't
null)
innerMap.put( null, map);//this is the line FB grumbles about - but should
not



Date: 2006-12-20 15:39
Sender: dbrosius


Do you have code that does a get on that map, and doesn't check for null?


Attached File

No Files Currently Attached

Changes ( 3 )

Field Old Value Date By
status_id Open 2007-02-25 17:07 alex319
close_date - 2007-02-25 17:07 alex319
category_id None 2007-01-17 16:11 wpugh