0

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

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属性的两种方式之间的区别。有人可以给我一个提示吗?

4

2 回答 2

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 构造函数创建的内容中。

于 2012-07-06T15:39:52.587 回答
0

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

list = new ListSchedule(cookie);

对我来说,它不会调用初始化您ArrayList<Schedule>并解释NullReferenceException

于 2012-07-06T15:54:33.493 回答