2012-07-18 80 views
0

嗨,我试图创建一个包含包含在我目前在该行得到一个空指针异常夹具日期的数组的JSONObject的机器人JSON if(fixturesDict.length() == 0){没有人知道为什么吗?for循环空指针异常

继承人我的代码

public void FillData() throws JSONException{  

     ListView list = getListView(); 
     list.scrollTo(0, 0); 
     list.setVerticalFadingEdgeEnabled(false); 

      list.setTextFilterEnabled(true); 

      LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 





     fixturesView = LayoutInflater.from(getBaseContext()).inflate(R.layout.fixturescell, 
       null); 


     //Log.v("MyFix", "fixturesArray = " + fixturesArray); 
     if(fixturesArray.length() < 1){ 

      TextView emptytext = (TextView) fixturesView.findViewById(R.id.TextView02); 
      emptytext.setText("No Upcoming Fixtures Available"); 

     }else{ 
     try{ 




      for(int t = 0; t < fixturesArray.length(); t++){ 
       JSONObject matchDateDict = fixturesArray.getJSONObject(t); 
       matchDate = matchDateDict.getString("matchdate"); 

       if(matchDatesHeadersArray.length() != 0){ 
        int lm = t - 1; 
        JSONObject lastDateDict = fixturesArray.getJSONObject(lm); 
        String lastMatchDate = lastDateDict.getString("matchdate"); 

        if(matchDate.equals(lastMatchDate)){ 
        } else { 
         matchDatesHeadersArray.put(matchDate); 
        } 
       } else { 
        matchDatesHeadersArray.put(matchDate);  
       }    
      } 

      JSONObject fixturesDict = new JSONObject(); 
      JSONArray tempArray = new JSONArray(); 
      JSONArray newTempArray = new JSONArray(); 
      JSONObject tempDict = null; 

      for(int f = 0; f < fixturesArray.length(); f++){ 
       String matchDateHeaderString = matchDatesHeadersArray.getString(f); 
       tempDict = fixturesArray.getJSONObject(f); 
       String currentMatchDate = tempDict.getString("matchdate"); 
       Log.v("myFix", "matchDateHeaderString = " + matchDateHeaderString + "currentMatchDate = " + currentMatchDate); 



*if(fixturesDict.length() == 0){* 


        tempArray = null; 
       } else { 
        tempArray = fixturesDict.getJSONArray(currentMatchDate); 
       } 

       if(!currentMatchDate.equals(matchDateHeaderString)){ 
        // ADD TEXT INTO ARRAY FOR HEADER TITLES 
       } 

       newTempArray.put(tempDict); 
       fixturesDict.put(matchDateHeaderString, newTempArray); 
       Log.v("myFix", "SEMI fixturesDict = " + fixturesDict); 

      } 

      Log.v("myFix", "FULL fixturesDict = " + fixturesDict); 



     }catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

继承人我得到

07-18 11:01:48.974: E/AndroidRuntime(26155): java.lang.NullPointerException 
07-18 11:01:48.974: E/AndroidRuntime(26155):at MIFixtures.FillData(MIFixtures.java:455) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at .MIFixtures$PostTask.onPostExecute(MIFixtures.java:390) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at e.MIFixtures$PostTask.onPostExecute(MIFixtures.java:1) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.os.AsyncTask.finish(AsyncTask.java:417) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.os.AsyncTask.access$300(AsyncTask.java:127) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.os.Looper.loop(Looper.java:143) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at android.app.ActivityThread.main(ActivityThread.java:4263) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at java.lang.reflect.Method.invokeNative(Native Method) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at java.lang.reflect.Method.invoke(Method.java:507) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
07-18 11:01:48.974: E/AndroidRuntime(26155): at dalvik.system.NativeStart.main(Native Method) 

我的继承人JSON提要错误

{ 
    "code":200, 
    "error":null, 
    "data":{ 
     "fixtures":[ 
      { 
       "kickoff":"15:00:00", 
       "matchdate":"2012-07-28", 
       "homescore":null, 
       "awayscore":null, 
       "attendance":null, 
       "homepens":null, 
       "awaypens":null, 
       "division_id":"5059", 
       "division":"Testing 1", 
       "comp":"LGE", 
       "location":null, 
       "fixture_note":null, 
       "hometeam_id":"64930", 
       "hometeam":"Team 1", 
       "awayteam_id":"64931", 
       "awayteam":"Team 2" 
      }, { 
       "kickoff":"15:00:00", 
       "matchdate":"2012-07-28", 
       "homescore":null, 
       "awayscore":null, 
       "attendance":null, 
       "homepens":null, 
       "awaypens":null, 
       "division_id":"5059", 
       "division":"Testing 1", 
       "comp":"LGE", 
       "location":null, 
+0

为了更好的帮助,请缩进/格式化您的代码,并在那里究竟发生异常时告诉我们。 – MByD 2012-07-18 10:09:29

+0

为logcat的说,你所得到的'onPostExecute(MIFixtures.java:390)'发布您的代码 – MAC 2012-07-18 10:10:56

+0

heyy卢克......你为什么要重复同样的问题NPE?这是这篇文章的副本http://stackoverflow.com/questions/11449803/android-adding-json-objects-into-an-array? – sunil 2012-07-18 10:20:06

回答

0

fixturesArray当你调用(fixturesArray.length() < 1)尚未创建

我建议检查变量fixturesArray,并确保已初始化当u调用

0

你好请参阅下面的代码

JSON字符串

{ 
"result": "success", 
"countryCodeList": 
[ 
    {"countryCode":"00","countryName":"World Wide"}, 
    {"countryCode":"kr","countryName":"Korea"} 
] 
} 

这里下面我获取国家信息

JSONObject json = new JSONObject(jsonstring); 
JSONArray nameArray = json.names(); 
JSONArray valArray = json.toJSONArray(nameArray); 

JSONArray valArray1 = valArray.getJSONArray(1); 

valArray1.toString().replace("[", ""); 
valArray1.toString().replace("]", ""); 

int len = valArray1.length(); 

for (int i = 0; i < valArray1.length(); i++) { 

Country country = new Country(); 
JSONObject arr = valArray1.getJSONObject(i); 
country.setCountryCode(arr.getString("countryCode"));       
country.setCountryName(arr.getString("countryName")); 
arrCountries.add(country); 
}