1

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);
Vodka
  • 113
  • 2
  • 10

1 Answers1

1

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

  • thx for answer, but i have like 100 field to set. should i really put ternary operator in 100 field that i have to set? – Vodka Aug 29 '20 at 02:36
  • nevermind the problem. i found out that i use ".toString() " on myDataClass.getBirthDate(). so it throws null pointer.because birthDate in database is null. sorry that's my foolishness. thanks for answer. i use ternary operator to validate it, then do .toString(). – Vodka Aug 29 '20 at 02:55