0

hi i'm trying to create an JSONObject that contains an array of dates that contain fixtures at the i'm currently getting a null pointer exception at this line if(fixturesDict.length() == 0){ does anyone know why?

heres my code

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

heres the error i'm getting

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)

heres my json feed

{
    "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,
8
  • For better help, please indent/reformat your code and tell us where exactly the exception occurs. Commented Jul 18, 2012 at 10:09
  • as logcat says you are getting NPE in onPostExecute(MIFixtures.java:390) post your code Commented Jul 18, 2012 at 10:10
  • heyy luke... why are you repeating the same question ? is this a duplicate of this post stackoverflow.com/questions/11449803/… ? Commented Jul 18, 2012 at 10:20
  • it's not a repeat question its the same subject but a different question. plus my code is different from the last question Commented Jul 18, 2012 at 10:23
  • I doubt you are using fixturesDict in if condition in that loop instead you need to use tempDict object. Commented Jul 18, 2012 at 10:27

2 Answers 2

0

Your fixturesArray haven't been created when you call (fixturesArray.length() < 1)

I suggest to check that variable fixturesArray and make sure it has been initialized when u call

Sign up to request clarification or add additional context in comments.

Comments

0

Hello Refer below code

JSON String

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

Here below I am fetching country details

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.