获取JSONarray时arning json及其nullpointer异常

时间:2013-01-02 13:32:33

标签: android json parsing nullpointerexception

我正在使用相同的代码/示例学习JSON及其在android中的解析     http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

但实现并运行其给出空指针异常

获取JSON,即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


 }

3 个答案:

答案 0 :(得分: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


 }

答案 1 :(得分:0)

确保json数组名称正确..使用

contacts = json.getJSONArray("contacts");

答案 2 :(得分:0)

你必须使用它:

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

如果你的响应有一个数组,它将返回一个数组,否则它将返回null

相关问题