Gson 将嵌套对象显示为 null

Gson showing nested Object as null

我正在尝试将对象反序列化为 JSON,一个对象包含一个嵌套对象。我为嵌套对象 (UserAddress) 和包含它的对象 (UserObjectWithNestedObject) 创建了一个实例。但是,当我序列化时,嵌套对象为空。所以我 运行 程序处于调试器模式以查看它何时变为 null。

当我在实例化 UserObjectWithNestedObject class 时将它用作字段时,它似乎为 null 但我不知道为什么,因为我在同一范围内的对象中实例化所以你会想他们将能够毫无问题地进行交流

抱歉,您可能需要缩放

我的class是

主要

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(这是变为 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 + '\'' +
                '}';
    }
}

你没有初始化它:)

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