In some scenarios for a blank string or a null as input validation is not happening as expected. Null or blank is getting through.
Either there is a bug in your validation logic, or you have data coming from a source that has not been subjected to validation.
If you posted the actual code and the NPE stacktrace, we could help you find it.
What does it mean?
It means that if StringUtils.isNotBlank()
throws an NPE, then there is a bug in the method. (The "side effect" that the javadoc is talking about is a logical side-effect; i.e. a secondary logical consequence of the way that the API is defined.)
It has got anything to do with my scenario?
It has nothing to do with it ... unless a call to StringUtils.isNotBlank()
is throwing an NPE.
UPDATE
Considering your example code (tweaked for illustrative purposes):
if (StringUtils.isNotEmpty(obj.getvalue())){
String s = obj.getvalue();
if (s == null) {
System.err.println("Ooops!!");
}
}
I can think of a number of scenarios in which this code could either give an NPE or cause "Ooops!" to be printed. For instance:
If
obj
isnull
then an NPE will definitely be thrown. This could happen before you calledisNotEmpty
, or (if the value of theobj
variable can be changed can be changed) in the "then" block.It is possible that the
getValue
method could throw an NPE.It is possible (but unlikely) that calling
getValue
has a side-effect that causes it to change the state ofobj
and return a different value (e.g.null
) on the 2nd call. Ooops!It is possible that some other thread is changing the state of the
obj
object while the current thread is executing the above code. Ooops!
Most of these problems could be "fixed" as follows:
String s = obj.getvalue();
if (StringUtils.isNotEmpty(s)){ // No NPE possible here now
if (s == null) {
System.err.println("Ooops!!"); // Cannot happen now.
}
}