Closed averbraeck closed 7 hours ago
This was issue https://github.com/averbraeck/djutils/issues/25.
Solved with the following code:
if (object == null)
{
if (message.matches("\\S+")) // \S+ is any non-whitespace character
throwMessage(NullPointerException.class, message + " may not be null", null);
else
throwMessage(NullPointerException.class, message, null);
}
Note that the private throwMessage(...)
method now accepts null in case there are no arguments to be passed. Earlier, the method needed a new ArrayList<>()
which was a waste of resources.
The ThrowTest
test class now also tests for the case where only a variable name (without any whitespace) is provided to the method.
Solved.
It is un-progammer-like to write
may not be null
for the frequent null-checks that are performed withThrow.whenNull()
. For example inPoint2d.closestPointOnSegment()
:Note also that the names are not correct in the provided messages. It is not possible in java to know what the name of a variable was at the level of the calling class. That is,
Throw
cannot know about the namessegmentPoint1
andsegmentPoint2
. Therefore, simplifying this toThrow.whenNull(segmentPoint1)
does not suffice.What we can do is the following:
This makes it very clear whether the name is correctly given to a programmer.
Throw
can figure out whether to add" may not be null"
by checking whether there is a blank in the provided message. Specifically using a regular expression pattern matching with\s
. Note that this is only done when in fact aNullPointerException
will be thrown. More elaborate messages, that should contain a\s
, are still possible in this way and will not be appended with" may not be null"
.