-2

I am developing an Android App, and I have a basic Customer class, which has a nationality data member which in turn is another public class. When I instantiate a new Customer both the nationality and birth date members (of non-primitive data types) are not set (calling their getters causes a null reference exception).

BTW, I'm using Android Studio, and I'm creating a new Customer instance through a JSON wrapper that receives the data from a RESTFUL web service call.

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

Nationality Class

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

Wrapper:

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

What am I missing here ?! Thanks in advance for the help.

FuSsA
  • 4,223
  • 7
  • 39
  • 60
  • 1
    Are you _really_ handling that exception? – Sotirios Delimanolis Nov 10 '17 at 17:31
  • you need to print that exception from catch block, so that any error can be tracked – vikas kumar Nov 10 '17 at 17:35
  • 1
    http://idownvotedbecau.se/nodebugging/ --- [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) --- *"What am I missing here ?"* Debugging your code to see what's going on. Or, if you want help, showing us the JSON text. – Andreas Nov 10 '17 at 17:35
  • What is in the JSON? I am not sure what you are trying to do here but make sure you don't have an empty Object that you are trying to get values from – Muhannad Nov 10 '17 at 17:43
  • Returned JSON : { "Customer": { "BirthDate": "/Date(551217600000+0400)/", "EmailAddress": null, "FirstName": "Someone", "Gender": 0, "Id": 1, "LastName": "Someone", "MobileNumber": "123456789", "Nationality": { "Id": 157, "NationalityAr": "Some Nat.", "NationalityEn": "Some Nat." }, "OfficeNumber": "123456789", } } – Mazin Kamal Nov 10 '17 at 18:05
  • @vikaskumar the exception is coming from another piece of code which tries to consume the returned customer object when calling getBirthDate or getNationality().getNationalityEn() for example. it's irrelevant to my problem. – Mazin Kamal Nov 10 '17 at 18:08

1 Answers1

0

Handle the exception in the catch block and print the exception stack to see exactly what is going on and where exactly the exception is coming from. Also, print your JSON object to make sure the data is actually received and your are getting the right properties.

TuaimiAA
  • 973
  • 6
  • 15
  • I'm not very sure, does the catch on his code ' catch (JSONException e)' handle Null Reference Exception or he needs to use a parent Exception class like 'catch (Exception e)' ? – Muhannad Nov 10 '17 at 17:55
  • This catch is to handle any JSON exception, which is never being reached since the JSON object received from the web service is intact and has no issues. The problem is, the returned object from the wrapper has both BirthDate & Nationality as null, while all other properties/data members are set to their correct values. The exception is coming from another piece of code which tries to consume the returned customer object and get it's nationality inner object. – Mazin Kamal Nov 10 '17 at 18:04
  • Handle both the JSON exception and NullPointerException by adding extra catch block. Generally, for exception handling, I would use a parent exception to handle unknown/unexpected exceptions that are not handled separately by their own catch blocks and use it to for logging proposes. From there I can print the exception stack and add specific catch blocks later on as necessary. Please add the exception stack that you are seeing to your question. – TuaimiAA Nov 10 '17 at 18:32
  • Thanks @TuaimiAA, that seems to be the right approach, will try and feedback. – Mazin Kamal Nov 10 '17 at 19:06