Java:实例化时未设置作为其他类实例的类数据成员

时间:2017-11-10 17:30:05

标签: java android class object instantiation

我正在开发一个Android应用程序,我有一个基本的Customer类,它有一个国籍数据成员,而这又是另一个公共类。当我实例化新客户时,未设置国籍和出生日期成员(非原始数据类型)(调用其getter会导致空引用异常)。

顺便说一下,我正在使用Android Studio,我正在通过一个从RESTFUL Web服务调用接收数据的JSON包装器创建一个新的Customer实例。

public class Customer
{
    // Private fields
    private int Id;
    private String FirstName;
    private String LastName;
    private String OfficeNumber;
    private String MobileNumber;
    private String EmailAddress;
    private boolean Gender;
    private Date BirthDate;
    private Nationality Nationality;

    /* Setters & Getters */
    public int getId() {
        return Id;
    }
    public void setId(int id) {
        Id = id;
    }

    public String getFirstName() {
        return FirstName;
    }
    public void setFirstName(String firstName) {
        FirstName = firstName;
    }

    public String getLastName() {
        return LastName;
    }
    public void setLastName(String lastName) {
        LastName = lastName;
    }

    public String getOfficeNumber() {
        return OfficeNumber;
    }
    public void setOfficeNumber(String officeNumber) {
        OfficeNumber = officeNumber;
    }

    public String getMobileNumber() {
        return MobileNumber;
    }
    public void setMobileNumber(String mobileNumber) {
        MobileNumber = mobileNumber;
    }

    public String getEmailAddress() {
        return EmailAddress;
    }
    public void setEmailAddress(String emailAddress) {
        EmailAddress = emailAddress;
    }

    public boolean getGender() {
        return Gender;
    }
    public void setGender(boolean gender) {
        Gender = gender;
    }

    public Date getBirthDate() {
        return BirthDate;
    }
    public void setBirthDate(Date birthDate) {
        BirthDate = birthDate;
    }

    public Nationality getNationality() {
        return Nationality;
    }
    public void setNationality(Nationality nationality) {Nationality = nationality; }
}

国籍班

public class Nationality
{
    // Private Fields
    private int Id;
    private String NationalityEn;
    private String NationalityAr;

    public Nationality(int id, String nationalityEn, String nationalityAr)
    {
        setId(id);
        setNationalityEn(nationalityEn);
        setNationalityAr(nationalityAr);
    }

    public Nationality(){}

    // Setters & Getters
    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getNationalityEn() {
        return NationalityEn;
    }

    public void setNationalityEn(String nationalityEn) {
        NationalityEn = nationalityEn;
    }

    public String getNationalityAr() {
        return NationalityAr;
    }

    public void setNationalityAr(String nationalityAr) {
        NationalityAr = nationalityAr;
    }
}

打包机:

public class JSONToObjectWarpper
{
    public Customer getCustomerObject(JSONObject jsonObject)
    {
        Customer customer = new Customer();

        try
        {
            JSONObject js = jsonObject.getJSONObject("Customer");
            Nationality nationality = new Nationality(
                    js.getJSONObject("Nationality").getInt("Id"),
                    js.getJSONObject("Nationality").getString("NationalityEn"),
                    js.getJSONObject("Nationality").getString("NationalityAr")
            );
            Date date = ConvertFromWCFDate(js.getString("BirthDate"));

            customer.setId(js.getInt("Id"));
            customer.setFirstName(js.getString("FirstName"));
            customer.setLastName(js.getString("LastName"));
            customer.setOfficeNumber(js.getString("OfficeNumber"));
            customer.setMobileNumber(js.getString("MobileNumber"));
            customer.setEmailAddress(js.getString("EmailAddress"));
            customer.setGender(js.getBoolean("Gender"));
            customer.setBirthDate(date);
            customer.setNationality(nationality);

        }
        catch (JSONException e)
        {
            // Handle the Exception Here
        }
        return customer;
    }
}

我在这里想念的是什么?在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

处理catch块中的异常并打印异常堆栈以确切了解发生了什么以及异常来自何处。此外,打印您的JSON对象以确保实际接收数据并获得正确的属性。