Reputation: 659
Does somebody know if there is a online code analyser for java. I would like to be able to check some small pieces of code.
Ex: this method has this warning: (Null Dereference)
private XMLGregorianCalendar asXMLGregorianCalendar(Date data) {
DatatypeFactory dateformat = null;
try {
dateformat = DatatypeFactory.newInstance();
} catch (MyException e) {
///
}
if (data == null) {
return null;
} else {
GregorianCalendar gregorianCal = new GregorianCalendar();
gregorianCal.setTimeInMillis(data.getTime());
return dateformat.newXMLGregorianCalendar(gregorianCal );
}
}
My new version is :
private XMLGregorianCalendar asXMLGregorianCalendar(Date data) throws ComponentBlockingException {
if (data == null) {
return null;
}
DatatypeFactory dateformat = null;
try {
dateformat = DatatypeFactory.newInstance();
} catch (MyException e) {
////
}
GregorianCalendar gregorianCal = new GregorianCalendar();
gregorianCal.setTimeInMillis(data.getTime());
return dateformat.newXMLGregorianCalendar(gregorianCal );
}
}
I think the second way should be ok.
Upvotes: 0
Views: 582
Reputation: 68715
I am not sure about any available online code anlayzer tool but let me try to help you with your code analysis.
If due to some reason following try block hits an an exception
try {
dateformat = DatatypeFactory.newInstance();
}
then your dateformat will remain null. So the following statement
return dateformat.newXMLGregorianCalendar(gregorianCal );
is prone to null pointer exception. And hence i believe you are getting static code anlayzer error.
You have to make sure dateformat is initialized or non-null in all the case before code reaches the line where you are doing the return.
Hope it helps!
Upvotes: 4