Duplicate: http://stackoverflow.com/questions/706263/where-to-check-if-an-object-is-null-or-not
should a check for null occur before making call to a function or within the function itself?
Duplicate: http://stackoverflow.com/questions/706263/where-to-check-if-an-object-is-null-or-not
should a check for null occur before making call to a function or within the function itself?
If you're going to use the object later on you should check Before making the call to the function, It will avoid an exception in the long run, but it really matters when the object is used
I would say within the call itself. That way, you only check for null in one place, not 5 places if you call that function from 5 different spots.
But this isn't a for sure answer. It really depends on the function and what the consequences are of calling it with a null argument.
If receiving a null on a function is a valid possibility, check for null within the function. Also DRY, and avoid having to check for null multiple times in the callers (so I second ryeguy's answer).
If receiving a null represents an error, fail (and fail early) with an exception (or let something like NullPointerException happen naturally).
If the exception occurs try to figure out why, and either: (i) remove the cause of the problem; or (ii) handle the exception properly