Discussion:
Possible null pointer dereference due to return value of called method
li xiaoming
2010-04-07 03:21:00 UTC
Permalink
public class Test {

public static void main(String[] args) {
ConcurrentHashMap<Long, Long> ls = new ConcurrentHashMap<Long, Long>();
for (long i=0; i<10; i++) {
ls.put(i, i);
}
for (long i=0; i<20; i++) {
if (ls.containsKey(i)) {
long num = ls.get(i);
System.out.println(i + "--->" + num);
}
}

}

}





Bug: Possible null pointer dereference due to return value of called method
Pattern id: NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE, type: NP, category: STYLE



The return value from a method is dereferenced without a null check, and the return value of that method is one that should generally be checked for null. This may lead to a NullPointerException when the code is executed.





why the code is taged by red throw NullPointerException?

_________________________________________________________________
Hotmail: Free, trusted and rich email service.
https://signup.live.com/signup.aspx?id=60969
Andrew Thorburn
2010-04-07 03:31:06 UTC
Permalink
��ǭ�隥�'�����'���y�l�.r����z۫����Z��-~�&��j��v'���k��.��1j{����y�e�G��x%��e����ib����k���Yi�)�z��q�m��ڲ�^�v��)��{����q�e�x-��h�Z���z�hv������-�(�q譲{���Z����k���YO�)�z�1q�m��ȟ*.�Ƭz[ zئk*޶��j{����y�p�'!�)eq��y�n�S��{^�L\z�b�|!z{ay�y�mzjm��jYe�xj[��{az�����v��y�H�z0j�/z�����iz{h���~��I硶��6�j˧r���k�^t����.�׿��๨�����1�x!�i_�����!�����l�欢v��'%y�����8h����m5��?�X���&�x%���h���8�+�r����bq�Z��޲�nnX��֭����ٚ�t��)�j�,
�ܺ�ޞ�ڲ��'��'�['{��˫����!1�K�x �x��h���]"��l��b��+����H��/���lr��j)�)좖���饲��,��騺�k�{e�/��遺��ț����Zh�{^�׫y��zwv筢�����j[����W���a��ڶ׫�'M<� ,�R8�1�D�55P PKr��Oq�^���I6 8^��n�{ږ矮����a�جu��}�ޝǝ�+a��Z��er���ݶ�z۫���������a�ج�w���l���v�z���&�r���_����T��ɚ�W�v�6�e>������ǩ�*'�����ע��^r�^wr���ע�֠y����m��06�e>������ǩ�*'�fj)E�筮�-y֧v���隊[���xg�����X�u���جr�,����x%��E�w[� ��.��,�g^v�m��?�f��f�r˦u�n�f��f��X��)ߣ�������b�ˬ
Muthuponmozhi Somasundaram
2010-04-07 06:30:19 UTC
Permalink
Hi,
This is coz the collection has the object reference whereas when we assign this to a primitive auto casting happens which can create nullpointer exception if the value is null.

Thanks
Muthu Ponmozhi
[cid:***@01CAD649.E5086610]<sip:***@infosys.com>

From: findbugs-discuss-***@cs.umd.edu [mailto:findbugs-discuss-***@cs.umd.edu] On Behalf Of li xiaoming
Sent: Wednesday, April 07, 2010 8:51 AM
To: findbugs-***@cs.umd.edu
Subject: [FB-Discuss] Possible null pointer dereference due to return value of called method


public class Test {
public static void main(String[] args) {
ConcurrentHashMap<Long, Long> ls = new ConcurrentHashMap<Long, Long>();
for (long i=0; i<10; i++) {
ls.put(i, i);
}
for (long i=0; i<20; i++) {
if (ls.containsKey(i)) {
long num = ls.get(i);
System.out.println(i + "--->" + num);
}
}
}
}


Bug: Possible null pointer dereference due to return value of called method
Pattern id: NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE, type: NP, category: STYLE


The return value from a method is dereferenced without a null check, and the return value of that method is one that should generally be checked for null. This may lead to a NullPointerException when the code is executed.


why the code is taged by red throw NullPointerException?
________________________________
Hotmail: Free, trusted and rich email service. Get it now.<https://signup.live.com/signup.aspx?id=60969>
Jason Irwin
2010-04-07 08:41:14 UTC
Permalink
Unlike other HashMaps, a ConcurrentHashMap cannot contain a null value, so
it is not possible for this code to ever return a "null" value so long as
the Map contains the key.

You have a guarding "if" which makes sure you only return a value when the
key exists, so a null will never be returned.

If a null was returned (for example, you remove the guarding "if" or use a
basic HashMap) then it is possible for the highlighted line to cause an NPE
due to auto-boxing (at runtime there will be an attempt to cast the null
Object to Long before converting to the primitive).



http://java.sun.com/javase/6/docs/api/java/util/concurrent/ConcurrentHashMap
.html



My guess is that it is simply a false positive caused by FB checking for
NPEs being caused by auto-boxing. Perhaps that detector needs refined to
account better for ConcurrentHashMaps and to allow for the guarding "if"?
Although it is probably safer for the detector to de rather strict, NPEs
caused by auto-boxing are a common problem.



J.



From: findbugs-discuss-***@cs.umd.edu
[mailto:findbugs-discuss-***@cs.umd.edu] On Behalf Of li xiaoming
Sent: 07 April 2010 04:21
To: findbugs-***@cs.umd.edu
Subject: [FB-Discuss] Possible null pointer dereference due to return value
of called method




public class Test {
public static void main(String[] args) {
ConcurrentHashMap<Long, Long> ls = new ConcurrentHashMap<Long, Long>();
for (long i=0; i<10; i++) {
ls.put(i, i);
}
for (long i=0; i<20; i++) {
if (ls.containsKey(i)) {
long num = ls.get(i);
System.out.println(i + "--->" + num);
}
}
}
}


Bug: Possible null pointer dereference due to return value of called method
Pattern id: NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE, type: NP, category:
STYLE


The return value from a method is dereferenced without a null check, and the
return value of that method is one that should generally be checked for
null. This may lead to a NullPointerException when the code is executed.


why the code is taged by red throw NullPointerException?

_____

Hotmail: Free, trusted and rich email service. Get it now.
<https://signup.live.com/signup.aspx?id=60969>

Loading...