0

嗨,我正在尝试创建一个 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,
4

2 回答 2

0

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

我建议检查该变量 fixturesArray并确保在您调用时它已被初始化

于 2012-07-18T10:19:39.003 回答
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);
}
于 2012-07-19T05:21:08.063 回答