title: android List View having jsonObject
body: <p>I am working on activity which request one php file on server &amp; this php file will return me one <code>JSONArray</code> having <code>JSONObjects</code> as its elements. I get this <code>jArray</code> &amp; extract out its contents  e.g., all <code>jsonObjects</code>. each jsonObject have data of one specific user like uniqueId, name, address, contact details etc... I showed a list of all users. I need when I click on list item it will show me complete profile of the concern user...&amp; for that i put these extracted <code>jsonObjects</code> in another <code>JSONObject</code> having uniqueId as name &amp; the corresponding extracted <code>jsonObject</code> as its value. </p>
  
  <p>My list :</p>
  
  <p><a href="http://i.stack.imgur.com/NoKDU.png" rel="nofollow">this</a>!</p>
  
  <p>When i click on one item of this list I get its <code>uniqueId</code> what I need is, all keys of <code>jsonObject</code> having <code>JsonObject</code> as its values. </p>
  
  <pre><code>    public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int position, long id) {
      ItemBean bean = (ItemBean) adapter.getItem(position);   
      Toast.makeText(this," FanId =&gt; " + bean.getUid() +" \n FanName =&gt; " + bean.getTitle(), 
                  Toast.LENGTH_SHORT).show();
      Iterator&lt;String&gt; userKeys=extracted();//here i get-&gt; Null PointerException!
          while(fKeys.hasNext()){
              System.out.println("Key:"+userKeys.next());
          }
    }
  
      //iterator that return me keys(e.g., all uids of users) 
      @SuppressWarnings("unchecked")
      private Iterator&lt;String&gt; extracted() {
          Iterator&lt;String&gt; keys = jFansData.keys();
          return keys;
      }
  
  
      12-10 20:45:55.833: ERROR/AndroidRuntime(907): Caused by:  java.lang.NullPointerException
      12-10 20:45:55.833: ERROR/AndroidRuntime(907): at com.technotalkative.listview5.MainActivity.extracted(MainActivity.java:87)
      12-10 20:45:55.833: ERROR/AndroidRuntime(907): at com.technotalkative.listview5.MainActivity.onCreate(MainActivity.java:51)
      12-10 20:45:55.833: ERROR/AndroidRuntime(907): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  </code></pre>
  
  <h2>I am not getting what I do wrong over here, any solution please...</h2>
  
  <h2>Here is the Complete Code Of My Class-></h2>
  
  <pre><code>public class MainActivity extends Activity implements OnItemClickListener {
  
  String url,fd;
  ListView mFansListView;
  JSONObject jFan_Data;//contain data of an indivisual fan
  JSONObject jFansData;//contain data of an all fans
  ListViewCustomAdapter adapter;
  LayoutInflater lInflater;
  private ArrayList&lt;Object&gt; fansList;
  private ItemBean bean;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      prepareArrayLits();
  
      mFansListView = (ListView) findViewById(android.R.id.list);
      adapter = new ListViewCustomAdapter(this, fansList);
      mFansListView.setAdapter(adapter);//come null pointer exception when no fan data is returned! hendle it...
  
      mFansListView.setOnItemClickListener(this);
  }
  
  @Override
  public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int position, long id) {
      // TODO Auto-generated method stub
      ItemBean bean = (ItemBean) adapter.getItem(position);
      Toast.makeText(this," FanId =&gt; " + bean.getUid() +" \n UID =&gt; " + bean.getTitle(), Toast.LENGTH_SHORT).show(); 
  
      Iterator&lt;String&gt; fKeys=extracted();
  
      while(fKeys.hasNext()){
          System.out.println("Key:"+fKeys.next());
      }
  }
  
  //....................................................................
  
  //iterator that return me keys(e.g., all uids of fans) through which i match clicked lists id
    @SuppressWarnings("unchecked")
  private Iterator&lt;String&gt; extracted() {
              Iterator&lt;String&gt; keys = jFansData.keys();//I get NullPointerExcertion over here...???
      return keys;
  }
  //.............................................................................
  
  /* *Method used to prepare the ArrayList,
   */
  public void prepareArrayLits()
  {
      url="http://192.168.200.111/ManU/location1_and.php";
      HttpClient client = new DefaultHttpClient();
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
  
      HttpResponse response;
      JSONObject json = new JSONObject();
   try {
      HttpPost post=new HttpPost(url);
       /**! Currently all these parameters are hard coded... 
        * In future i have to use services of LocationManager to get my current location!
        */
       json.put("radius", 1000);
       json.put("lat", 12.9716060);
       json.put("lang", 77.5903760);
       json.put("myid", "h9ow0");
  
          post.setHeader("json", json.toString());
          StringEntity se=new StringEntity(json.toString());
          se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
  
          post.setEntity(se);
  
          response=client.execute(post);
  
         /* Checking response */
          // Get the data in the entity
          InputStream in=response.getEntity().getContent(); 
  
          //convert it to string... 
           fd=convertStreamToString(in);
  
          Log.i("Read from Server", fd);
  
        //Checking that fd is array containing data of all users or have no data...
          String no_data="No Data"; 
  
            if(no_data==fd){
                Log.i("NoRecords", "No Users Near you... Try over long distance!");
                }else {    
  
                 JSONArray jArray = new JSONArray(fd);//contain array returned by "php" having data of all users
  
                     try{
                      fansList = new ArrayList&lt;Object&gt;();
                        for(int i=0;i&lt;jArray.length();i++){
                          //will return the data of each row fetched from JSONArray returned by location1.php
                            String data_of_each_user=jArray.get(i).toString();
  
                          Log.i("Data Of User at index "+i+" is", data_of_each_user);
  
                           //I put the object at index "i" into JSONObject &amp; retrieve data from name-value pair
                            jFan_Data=jArray.getJSONObject(i);//data of User at index i in array
  
                          AddObjectToList(jFan_Data.getString("uniqid").toString(),R.drawable.ic_add, jFan_Data.getString("name"), jFan_Data.getString("distance"));
  
                       try{
                              /** this JSONObject hold data of all users taking UniqueId of user as key &amp; 
                               * JSONObject at index i in the array returned by the php as the value!!! */      
                                 jFansData.put(jFan_Data.get("uniqid").toString(), jFan_Data);//why i getting NullPointerException Over Here...???
                            } catch (Exception e) {
                               // TODO: handle exception
                               e.printStackTrace(); 
                           }
  
                         }
                       } catch (Exception e) {
                           // TODO: handle exception
                           e.printStackTrace(); 
                       }
                  }   
       } catch (Exception e) {
         // TODO: handle exception
         e.printStackTrace(); 
      }
  
  }
  
  //..............................................................................
  // Add one item into the Array List
  public void AddObjectToList(String uid,int image, String title, String desc)
  {
      bean = new ItemBean();
      bean.setUid(uid);
      bean.setDescription(desc);
      bean.setImage(image);
      bean.setTitle(title);
      fansList.add(bean);
  }
  
  // -------------------------------------------------------------------------
  //convert respons into human readable formate
  private static String convertStreamToString(InputStream is) {
  
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      StringBuilder sb = new StringBuilder();
  
      String line = null;
      try {
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          try {
              is.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      return sb.toString();
   }
  }
  </code></pre>
  
  <p>as i mentioned above, I need to show the complete profile of the user when i click on list item... So, What i do to solve this problem!!! ???  </p>