I was wondering if the null object pattern could actually make debugging more difficult in certain cases. For example, if a method returns an "empty" object rather than a null then it will not throw an error. This is good in terms of reliable, clean code but couldn't it mean that there is now an empty object being used that essentially does nothing that could cause unexpected bugs. Rather than having a null exception to point the programmer to the problem there is now nothing to help locate it.
Yes it can because if your null object has to return a value (as in your example) you are just deferring the null issue and still need to check for null values.
I have found null object to be very helpful with two conditions are met.
A simple example is a callback.
class Callback {
void onDoSomething(SomeArg someArg);
}
Typically you would have to do a null check before using the callback
if (callback != null) {
callback.onDoSomething(someArg);
}
With the null object pattern you can have the default value of the callback be a null object and avoid the null checks in the rest of your code. Less boiler-plate code makes it easier to see the important parts.