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プロパティを初期化する2つの方法の違いをよく理解していません。誰かが私にヒントを教えてもらえますか?

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

2番目のコンストラクターでリストを初期化しない方法に注意してください。また、デフォルトのコンストラクターを呼び出すことから始めますが、後でオブジェクトへのポインターを、ListScheduleのStringコンストラクターから作成されるものに再割り当てします。

于 2012-07-06T15:39:52.587 に答える
0

あなたのコードはこのコンストラクターを呼び出しています:

list = new ListSchedule(cookie);

私にとって、あなたを初期化し、ArrayList<Schedule>それを説明するものを呼び出さないでくださいNullReferenceException

于 2012-07-06T15:54:33.493 に答える