java 检查对象是否有数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28622092/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:54:46  来源:igfitidea点击:

Check if Object has data or not?

javaobjectnull

提问by Java Curious ?

I am calling method to get data as per passed text that will ping into database.

我正在调用方法来根据传递的文本获取数据,这些文本将 ping 到数据库中。

So I want to check if there is data arrived in object or not.

所以我想检查是否有数据到达对象。

Code :

代码 :

Method m = service.getMethodDataByFilter(text);

I have tried :

我努力了 :

if(m == null){
    System.out.println("In NULL");
}

but it does not entering inside it.

但它没有进入里面。

I have also tried :

我也试过:

if(m.getName().isEmpty()){
    System.out.println("In NULL");
}

then it throws NPEin condition because no data received.

然后它抛出NPE状态,因为没有收到数据。

So how to check if there is data inside object or not ?

那么如何检查对象内部是否有数据呢?

Method Class :

方法类:

public class Method {
    private Integer id;
    private String name;
    // getter-setter
}

UPDATE

更新

Have just try Ruchira

刚试过Ruchira

    if(m== null){
        System.out.println("m is null");
    }else if(m.getName()==null){
        System.out.println("m.getName() is null");
    }else if(m.getName().isEmpty()){
        System.out.println("m.getName() is empty");
    }

Still throws same NPE.

还是抛出一样NPE

FULL Method Code :

完整方法代码:

public MethodDTO getMethod(String text){
    Method m = service.getMethod(text);

    if(m == null){
        System.out.println("m is null");
    }else if(m.getName()==null){
        System.out.println("m.getName() is null");
    }else if(m.getName().isEmpty()){
        System.out.println("m.getName() is empty");
    }

    ModelMapper mapper = ModelMap.methodMapper();
    return mapper.map(m, MethodDTO.class);
}

回答by TheLostMind

You need to change your code to :

您需要将代码更改为:

if(myObject==null){   // check if object is null
System.out.println("myObject is NULL");
}
else{ // myObject should not be null here
if(myObject.getField1()==null) // check for fields within the object
{
System.out.println("field1 is NULL");
}
if(myObject.getField2()==null)
{
System.out.println("field2 is NULL");
}
...
}

Note if the object is null, then trying to access any non-staticfield / method on it will result in NPE.

请注意,如果对象为空,则尝试访问其上的任何非静态字段/方法将导致 NPE。

回答by Ivaylo Strandjev

Try this if ( m == null || m.getName()== null). Note that apart from m its name may also be null.

试试这个if ( m == null || m.getName()== null)。请注意,除了 m 之外,它的名称也可能是null.

回答by Juned Ahsan

m == null

will return false when object mis not null but it does not check all its attributes recursively inside object.

当 objectm不为 null时将返回 false但它不会在 object 内部递归检查其所有属性。

m.getName()

seems to be returning null but even this statement will not throw NPE. But yes if you call m.getName().isEmpty()will throw NPE. You should better check like this:

似乎返回 null 但即使这个语句也不会抛出 NPE。但是是的,如果你打电话m.getName().isEmpty()会抛出 NPE。你最好这样检查:

if (m.getName() == null) //assuming you have already checked for m being null

回答by Ruchira Gayan Ranaweera

Explanation.

解释。

if(m == null){
  System.out.println("In NULL");
}

Since inside ifnot enter since mis not null. But

既然里面if进不去,既然m不是null。但

if(m.getName().isEmpty()){
  System.out.println("In NULL");
}

Here you get NullPointerExceptionsince m.getName()is null.

在这里你得到,NullPointerException因为m.getName()null

So you can try as follows

所以你可以尝试如下

 if(m == null){
  System.out.println("m is null");
 }else if(m.getName()==null){
  System.out.println("m.getName() is null");
 }else if(m.getName().isEmpty()){
  System.out.println("m.getName() is empty");
 }

回答by Sandeep Patange

Check the object is null or not like this:

检查对象是否为空,如下所示:

if(m != null && m.getName != null) {
 // Object m is not null and m.getName is also not null
} else {
 // Either Object m is null or m.getName is null or both are null
}