Prakruti Pathik
Prakruti Pathik

Reputation: 392

Is null check required here?

I come across this line of code in my project:

String statusRecoTime = sdf.format(cal.getTime());

Then there is a null check like

if (statusRecoTime != null) {
     //do something
}

I think that the statusRecoTime will never be null and there is no need of this check because it is assigned with an object.

Please let me know if my understanding is correct?

Upvotes: 1

Views: 444

Answers (4)

Anonymous
Anonymous

Reputation: 86130

I am assuming sdf is a SimpleDateFormat instance.

First thing to check is the documentation, as Mistalis already did in an answer. The one-argument format method is declared in the superclass DateFormat, and the documentation says

Returns:
the formatted time string.

This should be enough. There is no mention that it could return null, so there should be no need to check. We could stop here.

If you want to be even more sure, we can check the source code for the Java version you are using. In Java 8 (and I would expect, in all versions) format(Date) calls a three-argument format method, gets a StringBuffer back and calls its toString() method. StringBuffer.toString() makes a new String(). new is guaranteed never to return null. So now we can be sure.

Except: An evil person might write a subclass of SimpleDateFormat in which format(Date) may return null in conflict with the documentation. Your sdf variable could hold an instance of such an evil subclass. You could get null. If you know from your code that sdf is always a SimpleDateFormat and not some homegrown or third-party subclass, we can rule out this possibility.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1073968

Assuming that sdf is a SimpleDateFormat instance, format will never return null. The null check is completely unnecessary.

Various answers here concern themselves with whether that code will throw an exception. The only time it would is if sdf or cal were null. But assuming both are non-null and cal is a Calendar, that code won't throw an exception.

Upvotes: 0

Mistalis
Mistalis

Reputation: 18269

From the SimpleDateFormat docs, the method returns a NullPointerException if the passed date is null:

SimpleDateFormat.format()

Formats the given Date into a date/time string and appends the result to the given StringBuffer.

Specified by:
format in class DateFormat
Parameters: date - the date-time value to be formatted into a date-time string.
toAppendTo - where the new date-time text is to be appended.
pos - the formatting position. On input: an alignment field, if desired. On output: the offsets of the alignment field.
Returns: the formatted date-time string.
Throws: NullPointerException - if the given date is null.

Checking statusRecoTime != null is not necessary, as you will have an exception if null.

Upvotes: 2

Satish Kumar
Satish Kumar

Reputation: 110

No you do not need any null check here as if sdf will not be able to format the time it will throw error instead of null. although as you are formating time of calendar exception will also not come if you have defined correct format in sdf .

Upvotes: 0

Related Questions