如何在 getter 方法返回空值时而不是抛出 java.lang.NullPointerException。

huangapple go评论77阅读模式
英文:

how to return null value when getter have null value instead return java.lang.NullPointerException

问题

我查询数据库并将其映射到类中。

MyDataClass myDataClass = myDataDao.findMyDataByAccountNumber(accountNumber);

然后我将其映射到另一个类,如下所示。

MyOtherDataclass myOtherDataClass = new MyOtherDataClass();
myOtherDataClass.setBirthDate(myDataClass.getBirthDate() != null ? myDataClass.getBirthDate().toString() : null);

我知道 myDataClass.getBirthDate() 为空,因为数据库中的 birthdate 值为空。
如果我在数据库中插入了值到 birthdate,那么一切正常,都能正常工作。但如果它没有值,我不想抛出 java.lang.NullPointerException: null,而是希望在 JSON 中将其设置为 null。

我期望在 JSON 中的结果如下所示。

{
AccountNumber: "12313",
BlaBla: "blabla",
BirthDate: null,
}

找到了问题。

我在 myOtherDataClass.getBirthDate().toString() 上使用了 .toString(),因此它抛出了空指针异常,因为 myDataClass 中的 birthDate 为空。抱歉,这是我的错误。

通过以下方式解决:

myOtherDataClass.setBirthDate(myDataClass.getBirthDate() != null ? myDataClass.getBirthDate().toString() : null);
英文:

i query database and mapping it to class

MyDataClass myDataClass = myDataDao.findMyDataByAccountNumber(accountNumber);

and i map it to other class like this

MyOtherDataclass myOtherDataClass = new MyOtherDataClass();
myOtherDataClass.setBirthDate(myDataClass.getBirthDate().toString);

i know the myDataClass.getBirthDate() is null because birthdate value in my database is null.
if i insert value to birthdate in database, then its normal, everything works. but if it has no value, instead of throw java.lang.NullPointerException: null, i want to set it to null in json

i expect the result in json like this

{
AccountNumber: "12313",
BlaBla: "blabla",
BirthDate: null,
}

===========================================================================

Found out the problem.

i use .toString() on myOtherDataClass.getBirthDate().toString() so it throws null pointer.because birthDate in myDataClass is null. sorry that's my bad.

solved using

myOtherDataClass.setBirthDate(myDataClass.getBirthDate() != null ? myDataClass.getBirthDate().toString() : null);

答案1

得分: 1

我们对你的类(myDataClassmyOtherDataClass)没有任何可见性,所以很难确定为什么getter方法会抛出错误。尽管如此,我只是猜测,可能是你只想要一个快速的一行代码来处理空值,其中一个答案可能是:

myOtherDataClass.setName((myDataClass.getName() != null) ? myDataClass.getName() : "somethingelse");

这个三元运算符结构可以用来渲染任何你想要的内容作为 "somethingelse",包括null(我不知道你是如何生成JSON的,所以我不确定你是如何处理null的)。

如果你想看到一个完整的示例,这里有一个带有两个实例的虚拟DAO:

import java.util.*;
import java.lang.*;
import java.io.*;

class MyDataClass {
    // ... (与你提供的代码相同,省略)
}

class MyOtherDataClass {
    // ... (与你提供的代码相同,省略)
}

class Main {
    public static void main(String[] args) {
        // ... (与你提供的代码相同,省略)
    }
}

这是一个链接 在 mycompiler 上的链接

英文:

We don't have any visibility to your classes (myDataClass and myOtherDataClass) so it is hard to say for sure why the getter is throwing the error. Still, and I am just guessing here, it might be that you are just looking for a quick one liner to deal with the null value, one answer to which would be:

myOtherDataClass.setName((myDataClass.getName() != null) ? myDataClass.getName() : "somethingelse");

This ternary construct could be used to render whatever you want for "somethingelse", including a null (I don't know how you are generating json, either, so I don't know precisely how you are processing a null.)

If you would like to see an entire example in action, here one is with a fake DAO with two instances (one named, and one not named).

import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".

class MyDataClass {
    String accountNumber;
    String blaBla;
    String name;
    
    public MyDataClass(String accountNumber, String blaBla) {
        this.accountNumber = accountNumber;
        this.blaBla = blaBla;
    }
    
    public MyDataClass(String accountNumber, String blaBla, String name) {
        this.accountNumber = accountNumber;
        this.blaBla = blaBla;
        this.name = name;
    }
    
    public String getName() {
        return this.name;
    }
    
}

class MyOtherDataClass {
    String name;
    String foo;

    public MyOtherDataClass() {
        this.foo = "bar";
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void printIt() {
      System.out.println("Name: "+ name );
      System.out.println("Foo: " + foo );
      System.out.println("\n");
   }
}


class Main {
    public static void main(String[] args) {
        MyDataClass myDataClassHasNoName = new MyDataClass("12345","BlaBla");
        MyDataClass myDataClassHasName = new MyDataClass("12346","BlaBla","Name");

        MyOtherDataClass myOtherDataClassHasName = new MyOtherDataClass();
        MyOtherDataClass myOtherDataClassHasNoName = new MyOtherDataClass();
        myOtherDataClassHasName.setName((myDataClassHasName.getName() != null) ? myDataClassHasName.getName() : "somethingelse");
        myOtherDataClassHasNoName.setName((myDataClassHasNoName.getName() != null) ? myDataClassHasNoName.getName() : "somethingelse");
        
        myOtherDataClassHasName.printIt();
        myOtherDataClassHasNoName.printIt();
        
    }
}

Here's a link on mycompiler

huangapple
  • 本文由 发表于 2020年8月29日 07:49:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63642121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定