mithrop
mithrop

Reputation: 3353

References in Java constructor

This is the first version of my code :

public class ListSchedule implements ListInterface {

    private ArrayList<Schedule> list;

    private String cookie;

    public ListSchedule() {
        this.list = new ArrayList<Schedule>();
    }

    public ArrayList<Schedule> getList() {
        return list;
    }
}

In another class, I made this call :

protected final ListSchedule parse(String jsonString)
        throws CustomException {

    ListSchedule list = new ListSchedule();

    JSONArray schedulesArray;

    try {

        // Convert the response to a JSONObject
        JSONObject json = new JSONObject(jsonString);

        try {

            int errorCode = json.getInt("error");

            // Check if there is no error from FilBleu server
            if (errorCode > 0) {
                throw new CustomException(
                        CustomException.ERROR_FILBLEU,
                        "DataAccessObject", "Server error "
                                + json.getInt("subError"));
            }

            try {
                String cookie = json.getString("cookie");
                list = new ListSchedule(cookie);
            } catch (JSONException e) {
                throw new CustomException(CustomException.JSON_FORMAT,
                        "DataAccessObject", "No cookie value");
            }

            schedulesArray = json.getJSONArray("schedules");

            // NullPointerException with the line below
            Log.d("DAO", list.getList().toString());

            parseSchedulesArray(list, schedulesArray);

        } catch (JSONException e) { // Unable to get the error code
            throw new CustomException(CustomException.JSON_FORMAT,
                    "DataAccessObject", "Bad JSON format ("
                            + e.getMessage() + ")");
        }

    } catch (JSONException e) { // Unable to convert response
        throw new CustomException(CustomException.JSON_FORMAT,
                "DataAccessObject", "Bad JSON format ("
                        + e.getMessage() + ")");
    }

    return list;
}

then I had a NullPointerException from the line Log.d("DAO", list.getList().toString());. So I tried another solution. As you can see, the only difference is the initialization of the list property :

public class ListSchedule implements ListInterface {

    private ArrayList<Schedule> list = new ArrayList<Schedule>();

    private String cookie;

    public ListSchedule() {
    }

    public ArrayList<Schedule> getList() {
        return list;
    }
}

and the NullPointerException was never thrown again...

I don't really understand the difference between the two ways of initializing the list property. Can somebody give me a hint please ?

Upvotes: 0

Views: 121

Answers (2)

GETah
GETah

Reputation: 21409

You code is calling this constructor:

list = new ListSchedule(cookie);

Which to me, does not call the one that initializes your ArrayList<Schedule> and that explains the NullReferenceException

Upvotes: 0

Woot4Moo
Woot4Moo

Reputation: 24316

I am speculating that the following constructor exists in your code base :

public ListSchedule(String cookie) {
        this.cookie = cookie;
    }

and what you need is the following:

     public ListSchedule(String cookie) {
                this.cookie = cookie;
                this.list = new ArrayList<Schedule>();
            }

This is further validated by the invocation of this line in your program:

list = new ListSchedule(cookie);

Notice how you don't initialize the list in the second constructor. Also you start by invoking the default constructor, but you later reassign the pointer to the object into what gets created from the String constructor of ListSchedule.

Upvotes: 4

Related Questions