2012-07-06 79 views
0

这是我的代码的第一个版本:参考Java中构造

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

然后我就从线Log.d("DAO", list.getList().toString());一个NullPointerException 。所以我尝试了另一种方法。正如你所看到的,唯一的区别是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属性。有人可以给我一个提示吗?

+1

,如果你无法理解的差异,你应该阅读一个基本的程序手册 – Blackbelt 2012-07-06 15:39:28

+1

@blackbelt:我实际上不理解带有NullPointerException的行为。 – mithrop 2012-07-06 15:41:42

+1

@blackbelt:不是。他的代码显然是正确的。事实上,我之前看到过这种奇怪的行为,找不到一个好的解释。他显然调用构造函数,并且列表应该被初始化,但事实并非如此。 – Tudor 2012-07-06 15:41:52

回答

4

我猜测,下面的构造函数代码库中的存在:

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

,你需要的是以下几点:

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

这是通过这条线在调用进一步验证你的程序:

list = new ListSchedule(cookie); 

请注意你如何不在第二个c中初始化列表onstructor。此外,您首先调用默认的构造函数,但您稍后将指向该对象的指针重新分配为从ListSchedule的String构造函数创建的对象。

+3

我认为这是一个错字。 – Tudor 2012-07-06 15:40:33

+0

@Tudor足够公平 – Woot4Moo 2012-07-06 15:40:57

+0

我只是在复制/粘贴,现在修复错误;) – mithrop 2012-07-06 15:41:00

0

您的代码调用此构造:

list = new ListSchedule(cookie); 

这些对我来说,不调用初始化你ArrayList<Schedule>的一个和解释NullReferenceException

+0

实际上,这将实例化构造函数做的ListSchedule对象。 – Woot4Moo 2012-07-06 15:56:18

+0

@ Woot4Moo没有我的意思是'ArrayList '而不是'ListSchedule'。对困惑感到抱歉 – GETah 2012-07-06 15:58:38