java 想测试一个参数值在查询字符串中是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2378015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Want to test that a parameter value is null in querystring
提问by Ankur
I am using a servlet to do a range of things.
我正在使用 servlet 来做一系列事情。
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.
如果特定参数“t”不存在,我想做某些类型的处理,如果不存在,我想做不同的事情。
I am trying to test the parameter t (which I save as a String called editType), using the following condition:
我正在尝试使用以下条件测试参数 t(我将其另存为名为 editType 的字符串):
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.
我如何正确地进行这个测试,因为我遇到了空指针异常的问题,因为我的代码似乎总是期望“t”的值我需要能够创建一个不期望 t 的条件(允许它为空),并在其中进行处理。
回答by Adam Batkin
You need to re-order your comparison a bit, in particular to check for nullfirst:
您需要对比较重新排序,特别是首先检查null:
String editType = request.getParameter("t");
if(editType!=null && !editType.isEmpty()){
//processing here
}
The problem you are having is that Java is trying to call the isEmpty()method on a nullobject, which is why you need to check for nullfirst.
您遇到的问题是 Java 试图isEmpty()在null对象上调用该方法,这就是您需要先检查的原因null。
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 entire ifstatement and won't try to call isEmpty()(which would result in a NullPointerException).
Java 使用Short-circuit evaluation,这意味着它会从左到右检查您的条件,一旦找到可以决定整个条件结果的条件,它就会停止评估。因此,如果editType最终为 null,Java 将停止评估整个if语句并且不会尝试调用isEmpty()(这将导致 a NullPointerException)。
回答by Péter T?r?k
Ah, I see you tagged nullpointerexceptionso I make a guess. You should test for null first, then emptiness after:
啊,我看到你被标记了,nullpointerexception所以我猜一下。您应该先测试 null,然后测试为空:
if (editType != null || !editType.isEmpty()) {
//processing here
}
However, this will evaluate to true if editTypeis an empty string. To get the correct result, you should use AND (&&) instead of OR (||), like this:
但是,如果editType是空字符串,这将评估为真。要获得正确的结果,您应该使用 AND (&&) 而不是 OR (||),如下所示:
if (editType != null && !editType.isEmpty()){
//processing here
}
回答by sfussenegger
You should change this to
你应该把它改成
String editType = request.getParameter("t");
if(editType != null && !editType.isEmpty()){
//processing here
}
It's useful to have some StringUtil class that offers an ifEmpty() method as this is quite common:
拥有一些提供 ifEmpty() 方法的 StringUtil 类很有用,因为这很常见:
public static final class StringUtil {
private StringUtil() {
}
public static boolean isEmpty(String s) {
return s != null && !s.isEmpty();
}
}
Than change you're code to
比改变你的代码
String editType = request.getParameter("t");
if(!StringUtil.isEmpty(editType)){
//processing here
}
回答by fastcodejava
String editType = request.getParameter("t");
if(!(editType == null || editType.isEmpty())){
//processing here
}
To some people this reads better.
对某些人来说,这读起来更好。
回答by Winston Chen
You will have a problem running this code.
您将在运行此代码时遇到问题。
It's better this way:
这种方式更好:
String editType = request.getParameter("t");
if(editType!=null&&!editType.isEmpty()){
//processing here
}
null test has to be in the front to avoid the method invocation on null.
null 测试必须在前面,以避免对 null 的方法调用。
回答by DaveJohnston
First of all the conditions in your if statement are evaluated from left to right. So the first thing it checks is editType.isEmpty() and if editType is null you will have a null pointer exception. So the first part of your if should be editType != null (preferred to !(editType == null)).
首先,if 语句中的条件是从左到右计算的。所以它检查的第一件事是 editType.isEmpty() 并且如果 editType 为 null 你将有一个空指针异常。所以你的 if 的第一部分应该是 editType != null (首选 !(editType == null))。

