0

I am learning JSON and its parsing in android using same code/example
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

but while implementing and running its give null pointer exception

while getting JSON i.e on contacts = json.getJSONArray(TAG_CONTACTS);

  JSONArray contacts = null;
 private static String url = "http://api.androidhive.info/contacts/";


 public void initParsing()
 {
    ListView listView = (ListView) findViewById(R.id.listView_song);
     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
     JSONParser jParser = new JSONParser();
     JSONObject json = jParser.getJSONFromUrl(url);
     try {
            // Getting Array of Contacts
            System.out.println("---getJSON");
            contacts = json.getJSONArray(TAG_CONTACTS);

            // looping through All Contacts
            for(int i = 0; i < contacts.length(); i++){
                JSONObject c = contacts.getJSONObject(i);
                 System.out.println("---times");
                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String email = c.getString(TAG_EMAIL);
                String address = c.getString(TAG_ADDRESS);
                String gender = c.getString(TAG_GENDER);

                // Phone number is agin JSON Object
                JSONObject phone = c.getJSONObject(TAG_PHONE);
                String mobile = phone.getString(TAG_PHONE_MOBILE);
                String home = phone.getString(TAG_PHONE_HOME);
                String office = phone.getString(TAG_PHONE_OFFICE);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_EMAIL, email);
                map.put(TAG_PHONE_MOBILE, mobile);

                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

//       ListAdapter adapter = new SimpleAdapter(this, contactList,
//                  R.layout.list_item,
//                  new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
//                          R.id.name, R.id.email, R.id.mobile });

       // setListAdapter(adapter);
    //  listView.setAdapter(new CustomArrayAdapter(this, TAG_NAME)); // setting the adapter


 }
2
  • 1
    show us the logcat output and where exactly the app is crashing Commented Jan 2, 2013 at 13:43
  • did you seriously commented on your own question a minute after asking it ? Commented Jan 2, 2013 at 14:04

3 Answers 3

1
public void initParsing()
 {
    ListView listView = (ListView) findViewById(R.id.listView_song);
     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
     JSONParser jParser = new JSONParser();
     JSONObject json = jParser.getJSONFromUrl(url);
     try {
            // Getting Array of Contacts
            System.out.println("---getJSON");
           if(json != null && json.has("contacts")){
            contacts = json.getJSONArray("contacts");

            // looping through All Contacts
            for(int i = 0; i < contacts.length(); i++){
                JSONObject c = contacts.getJSONObject(i);
                 System.out.println("---times");
                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String email = c.getString(TAG_EMAIL);
                String address = c.getString(TAG_ADDRESS);
                String gender = c.getString(TAG_GENDER);

                // Phone number is agin JSON Object
                JSONObject phone = c.getJSONObject(TAG_PHONE);
                String mobile = phone.getString(TAG_PHONE_MOBILE);
                String home = phone.getString(TAG_PHONE_HOME);
                String office = phone.getString(TAG_PHONE_OFFICE);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_EMAIL, email);
                map.put(TAG_PHONE_MOBILE, mobile);

                // adding HashList to ArrayList
                contactList.add(map);
            }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

//       ListAdapter adapter = new SimpleAdapter(this, contactList,
//                  R.layout.list_item,
//                  new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
//                          R.id.name, R.id.email, R.id.mobile });

       // setListAdapter(adapter);
    //  listView.setAdapter(new CustomArrayAdapter(this, TAG_NAME)); // setting the adapter


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

3 Comments

if i write else part of if(json != null && json.has("contacts")) it is going in that but looking at the json by hitting that link it has the contacts.... can anyone explain what is happening here
Could you tell me what is the use of else part, if you have contacts JSONARRAY only then there is need to parse the response, isn't it?
Hey friend the problem is resolve many thanks to you and others
0

Make sure the json array name is right..try using

contacts = json.getJSONArray("contacts");

2 Comments

by hitting the link from where i am getting JSON array, it has the same
no change same issue, problem is somewhere else or i am missing something?
0

You must use this:

contacts = json.optJSONArray("array name");

this will return an array if your response having an array otherwise it will return null.

5 Comments

thanks for your suggestion brother but issue remains the same
Are you sure you are getting an array values.
Could you post your JSON response.
you must not. both methods exist, in some case test for null is good, in some other, you expect an exception because your app cannot run without data.
thanks a lot friend the issue is resolved now.... i did little research and effort by own and solution came.. thanks a lot

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.