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);