Does anybody know why String.compareTo
is not programmed to behave more graciously in regards to a null
parameter?
In my opinion the next sequence should return "1" or at least the javadoc should be more specific in regards to the NPE. equals()
returns false
in this case and I guess equals
and compareTo
should be consistent.
E.g.
String nullString = null;
System.out.println ("test".equals(nullString));
System.out.println ("test".compareTo(nullString));
Is throwing a NPE:
false
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.compareTo(String.java:1177)
To add to it. If you were to make this compareTo in your code, would have you verified for nulls?