Azat Usmanov
Azat Usmanov

Reputation: 103

FindBugs Null passed for non-null parameter of new java.math.BigInteger

In the bellow snippet why does FindBugs complain about null being passed to a BigInteger constructor? It's says its known null at line: signature=null;

byte[] signature;
Calendar today = new GregorianCalendar(
                        now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE),
                        now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
Date d=today.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timestamp =sdf.format(d).toString();
String pp=user.userName+secretKeyStr+timestamp;
try {
    signature = Security.getMD5Hash(pp);
} catch (Exception e) {
    e.printStackTrace();
    signature=null;
}
//convert byte[] into String. method to generate MD5 hash of a string in Java
BigInteger bigInt = new BigInteger(1,signature); 

is it a false positive ot am I missing something here? should I return null on Exception?

Upvotes: 1

Views: 3722

Answers (1)

marstran
marstran

Reputation: 28046

It is known that signature can be null at the new BigInteger(1,signature); line. That is probably a bug because the constructor of BigInteger will throw a NullPointerException if one of the parameters is null.

You should handle your exception in some other way, for example by returning null (as you say). In my opinion, you should also catch a more specific exception than Exception.

Upvotes: 3

Related Questions