android JSON for循环空指针异常

时间:2012-07-18 10:06:08

标签: android json

嗨,我正在尝试创建一个JSONObject,其中包含一个日期数组,其中包含我目前在此行if(fixturesDict.length() == 0){处获得空指针异常的装置有没有人知道原因?

继承我的代码

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,

2 个答案:

答案 0 :(得分:0)

致电fixturesArray

时,您的(fixturesArray.length() < 1)尚未创建

我建议检查变量fixturesArray并确保在调用

时已初始化该变量

答案 1 :(得分: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);
}