Java构造函数中的引用

时间:2012-07-06 15:36:52

标签: java android

这是我的代码的第一个版本:

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

在另一节课中,我做了这个电话:

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

然后我从NullPointerException行获得Log.d("DAO", list.getList().toString());。所以我试了另一个解决方案如您所见,唯一的区别是list属性的初始化:

public class ListSchedule implements ListInterface {

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

    private String cookie;

    public ListSchedule() {
    }

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

并且NullPointerException再也没有被抛出......

我真的不明白初始化list属性的两种方法之间的区别。有人可以给我一个提示吗?

2 个答案:

答案 0 :(得分:4)

我推测你的代码库中存在以下构造函数:

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

以及您需要的是:

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

通过在程序中调用此行进一步验证:

list = new ListSchedule(cookie);

注意如何在第二个构造函数中初始化列表。您也可以从调用默认构造函数开始,但稍后将指向该对象的指针重新分配到从ListSchedule的String构造函数创建的内容中。

答案 1 :(得分:0)

您的代码正在调用此构造函数:

list = new ListSchedule(cookie);

对我而言,不会调用初始化ArrayList<Schedule>并解释NullReferenceException

的那个