Im trying to deserialize objects into JSON, one object contains a nested object. I created an instance of both the nested object (UserAddress
)and the one containing it (UserObjectWithNestedObject
). However, when I serialize, the nested object is null. So I ran the program in debugger mode to see when it goes null.
It seems to be null when I use it as a field when instantiating the UserObjectWithNestedObject
class but I dont know why because I instantiated in the objects in the same scope so you’d think the they would be able to communicate with no issue
My classes
Main
import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { serializeUserObjectWithNestedObject(); } public static void serializeUserObjectWithNestedObject(){ UserAddress userAddress = new UserAddress( "std", "000", "Dulin", "Ireland" ); UserObjectWithNestedObject uowno= new UserObjectWithNestedObject( "thanss", "j@f.com", 21, true, userAddress ); System.out.println( "nthis is the UserAddress object "+userAddress); String theJson = new Gson().toJson(uowno, UserObjectWithNestedObject.class); System.out.println(theJson); } }
UserObjectWithNestedObject
public class UserObjectWithNestedObject { private String name; private String email; private int age; private boolean isDeveloper; //This is the object inside this object public UserAddress userAddress; public UserObjectWithNestedObject(String name, String email, int age, boolean isDeveloper, UserAddress userAddress) { this.name = name; this.email = email; this.age = age; this.isDeveloper = isDeveloper; } @Override public String toString() { return "UserObjectWithNestedObject{" + "name='" + name + ''' + ", email='" + email + ''' + ", age=" + age + ", isDeveloper=" + isDeveloper + ", userAddress=" + userAddress + '}'; } }
UserAddress (this is the nested object that turns null)
public class UserAddress { private String street; private String houseNumber; private String city; private String country; public UserAddress( String houseNumber, String city, String country, String street) { this.houseNumber = houseNumber; this.city = city; this.country = country; this.street = street; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getHouseNumber() { return houseNumber; } public void setHouseNumber(String houseNumber) { this.houseNumber = houseNumber; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public String toString() { return "UserAddress{" + "street='" + street + ''' + ", houseNumber='" + houseNumber + ''' + ", city='" + city + ''' + ", country='" + country + ''' + '}'; } }
Advertisement
Answer
You did not initialize it 🙂
public UserObjectWithNestedObject(String name, String email, int age, boolean isDeveloper, UserAddress userAddress) { this.name = name; this.email = email; this.age = age; this.isDeveloper = isDeveloper; this.userAddress = userAddress; }