3
if( currentTimeslot.isEmpty()){
         System.out.println("Do stuff");
            }

我怎么得到一个NullPointerException?如何检查字符串是否存在NULL,如果存在则执行操作?每当currentTimeslot等于NULL我得到错误。这是控制台消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PM.ui.MainFrame.getJPanelTopMenu(MainFrame.java:382)
4

6 回答 6

5

尝试以下

    if( currentTimeslot == null){
             System.out.println("Do stuff");
   }
于 2013-01-31T15:12:10.070 回答
2

An important thing to ask yourself: Are null and empty logically equivalent for what you're doing?

If so, you might want to use this:

if (currentTimeslot == null || currentTimeslot.isEmpty()) {
    // Do stuff
}

If the first half of that statement evaluates to true, Java won't bother with the second half, thus protecting you from the null-pointer exception.

Another approach would be to normalize your data; if you want null and an empty string to be treated as the same thing, just do something like this early in the code:

if (currentTimeslot == null) currentTimeslot = "";

Now you don't have to defensively null-check every time you use it.

As to why you're getting the exception: in Java, all objects (any variable that isn't a primitive type like int, boolean, etc.) is null until you initialize* it. If you try to access any methods or fields of that object, you'll get a null-pointer exception because you're asking the code to access something that doesn't actually exist yet. In Java, you either want to make sure your objects get initialized to something early or do a lot of defensive null-checking (either with if (variable != null) { ... } or try { ... } catch (NullPointerException npe) { ... } blocks) to prevent exactly the problem you're running into.

*-- Initialize it with something other than null, of course.

于 2013-01-31T15:30:25.083 回答
1
if(currentTimeslot==null){
    System.out.println("whatever");
}

该命令currentTimeslot.isEmpty()与 相同currentTimeslot.equals(""),它不被视为空,它只是空的。如果要检查它是否为空或为空,则必须将一个 if-case 放入另一个 if-case,如下所示:

if(currentTimeslot==null){
    System.out.println("null string");
}
else{
    if(currentTimeslot.isEmpty()){
       System.out.println("empty string");}
     }

如果要放置的命令很多要复制两次,则可以将它们放入函数中并调用该函数,或者使用在两种情况下都为真的布尔变量,然后检查布尔变量是否为真其余的命令,如下所示:

 boolean empty;
if(currentTimeslot==null){
    System.out.println("null string");
    empty=true;
}
else{
    if(currentTimeslot.isEmpty()){
       empty=true;
       System.out.println("empty string");}
     }
 if(empty){
 ....
 .... }

希望这可以帮助 :)

于 2013-01-31T15:55:15.003 回答
1

您可能是 Java 新手,在 Java 中,对象可以为 null,这意味着该对象上没有可接近的方法,因此每次尝试访问 null 对象的方法时都会抛出 NullPointerException。

要解决此问题,您应该检查 Object is not null by

        if ( currentTimeslot != null ){
            ....
        }

由于所有Java 对象都从 java.lang.Object 扩展而来,因此此检查不仅适用于 String,而且适用于任何类型。

于 2013-01-31T15:19:29.923 回答
0

If you're getting a null pointer exception, it could be because currentTimeslot is null. So you should first check if it's not null and then invoke one of its methods:

if(currentTimeslot!=null && currentTimeslot.isEmpty())...
于 2013-01-31T15:23:38.503 回答
0

我建议你放弃大多数字符串空和空测试。 Apache Commons Lang StringUtils很久以前就解决了大多数字符串操作(为空、为空、不为空、全为空等)。

下载 apache commons lang 并使用它。

你的代码会变成这样:

if (StringUtils.isEmpty(currentTimeslot))
{
   ... react to an empty currentTimeslot .
}
else // currentTimeslot  is not emty (that includes not null).
{
   .. react to a not empt currentTimeslot 
}
于 2013-01-31T15:43:29.173 回答