I am using a servlet to do a range of things.
If a particular parameter “t” does not exist I want to do certain types of processing, if it does I want to do different things.
I am trying to test the parameter t (which I save as a String called editType), using the following condition:
String editType = request.getParameter("t");
if(!editType.isEmpty()||!(editType==null)){
//processing here
}
How do I do this test properly, because I am getting the problem of a nullpointerexception, because my code always seems to expect a value for “t” I need to be able to create a condition which doesn’t expect t (allows it to be null), and do processing in that.
You need to re-order your comparison a bit, in particular to check for
nullfirst:The problem you are having is that Java is trying to call the
isEmpty()method on anullobject, which is why you need to check fornullfirst.Java uses Short-circuit evaluation, meaning it will check your conditions from left to right, and as soon as it finds one that would let it decide the result of the entire condition, it will stop evaluating. So if
editTypeends up being null, Java will stop evaluating the entireifstatement and won’t try to callisEmpty()(which would result in aNullPointerException).