title: Errors when getting JSON result into ListView
body: <p><em><strong>UPDATE 4: Problem Solved Check my Answer Below</em></strong></p>
  
  <p>I am trying to retreive JSON result from PHP Webservice and save it into ListView But I got an errors </p>
  
  <p>I'm not sure what is my mistake could you help me please </p>
  
  <p>I was following this tutorial from Travis (mybringback)</p>
  
  <p><a href="http://www.mybringback.com/tutorial-series/13239/android-mysql-php-json-part-6-json-parsing-and-android-design/" rel="nofollow">http://www.mybringback.com/tutorial-series/13239/android-mysql-php-json-part-6-json-parsing-and-android-design/</a></p>
  
  <p>His method (Only on this part) doesn't require any Parameters to get Posts (and it is actually workis using HIS PHP Method) 
  But My web Service Method require 2 Parameters (kioskid (device ID), accesstoken) which are saved in SharedPreferense</p>
  
  <p><del>Update1: </p>
  
  <p>I modified the Android code also uninstall the old one and start from beginig (Login and so on) until i get to EventListActivity But no List is showing and I got only one error Line in Log (no error message  in the device)</del></p>
  
  <p><del>Update2: After Modifying the code and separating <strong><em>UpdateJSON()</em></strong> and trying to get JSON Response in  <strong><em>doInBackground()</em></strong> </del></p>
  
  <p><del>update 3: I Added JSONParse Class </del></p>
  
  <p>I got a new Error message  that I'm getting NullPointerexception at <strong><em>setListAdapter(adapter)</em></strong> inside the method <strong><em>updateList()</em></strong></p>
  
  <p><strong>this is the error Log:</strong></p>
  
  <pre><code>03-26 16:58:38.397: E/AndroidRuntime(16196): FATAL EXCEPTION: main
  03-26 16:58:38.397: E/AndroidRuntime(16196): Process: com.test.event, PID: 16196
  03-26 16:58:38.397: E/AndroidRuntime(16196): java.lang.NullPointerException
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at android.widget.ListView.setAdapter(ListView.java:480)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at android.app.ListActivity.setListAdapter(ListActivity.java:265)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at com.decoder.tapway.event1.EventListActivity.updateList(EventListActivity.java:140)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at com.decoder.tapway.event1.EventListActivity.access$3(EventListActivity.java:136)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at com.decoder.tapway.event1.EventListActivity$LoadEvents.onPostExecute(EventListActivity.java:221)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at com.decoder.tapway.event1.EventListActivity$LoadEvents.onPostExecute(EventListActivity.java:1)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at android.os.AsyncTask.finish(AsyncTask.java:632)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at android.os.Handler.dispatchMessage(Handler.java:102)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at android.os.Looper.loop(Looper.java:136)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at android.app.ActivityThread.main(ActivityThread.java:5050)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at java.lang.reflect.Method.invoke(Method.java:515)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  03-26 16:58:38.397: E/AndroidRuntime(16196):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><strong>this is the PHP function (Yii Framework) that I'm trying to get its JSON result</strong></p>
  
  <pre><code>public function actionEventList(){
          if(isset($_POST['kioskid']) &amp;&amp; isset($_POST['accesstoken'])){
              $response = array();
  
              $kiosk = Kiosk::model()-&gt;findByAttributes(array('kioskid'=&gt;$_POST['kioskid'], 'accesstoken'=&gt;$_POST['accesstoken']));
              if($kiosk === null){
                  $response['status'] = '0';
                  $response['message'] = 'Invalid Kiosk';
              } else {
                  $events = Event::model()-&gt;findAllByAttributes(array('createdby'=&gt;$kiosk-&gt;userid, 'status'=&gt;'A'));
                  $list = array();
                  foreach($events as $row){
                      $list[] = array('id'=&gt;$row-&gt;id, 'name'=&gt;$row-&gt;eventname);
                  }
                  $response['status'] = '1';
                  $response['list'] = $list;
              }
              $this-&gt;_sendResponse(200, CJSON::encode($response));
          } else {
              $this-&gt;_sendResponse(400);
          }
      }
  </code></pre>
  
  <p><strong>This is my Java code in android: (UPDATED 2)</strong></p>
  
  <p>The Most Important Method is  <strong><em>updateJSONdata()</em></strong> that Retrieves recent Events from the server</p>
  
  <p>Then I call that method inside <strong><em>doInBackground()</em></strong></p>
  
  <pre><code>public class EventListActivity extends ListActivity {
  
      // Progress Dialog
      private ProgressDialog pDialog;
  
  
      // testing on Server:
      private static final String EVENT_LIST_URL = "http://www.XXX.com/XXX/XXX/eventList";
  
  
  
      // JSON IDS:
      private static final String TAG_STATUS = "status";
      private static final String TAG_EVENT_LIST = "list";
      private static final String TAG_EVENT_ID = "id";
      private static final String TAG_EVENT_NAME = "name";
      private static final String TAG_MESSAGE = "message";
  
      // An array of all of our Events
      private JSONArray jaEvents = null;
  
      // manages all of our events in a list.
      private ArrayList&lt;HashMap&lt;String, String&gt;&gt; arrayEventList;
  
  
      // prerepare SharedPreferences file name
      private static final String PREFS_NAME = "com.MyApp.event"; 
  
      private SharedPreferences sharedPref ;
  
      String accesstoken, username, kioskid,spKioskid, password ;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.event_list_layout);
  
          // setup SharedPreferences file
          this.sharedPref = getSharedPreferences(PREFS_NAME,0);
  
          // get data from previous activity 
          this.accesstoken = sharedPref.getString("accesstoken", null);
          this.kioskid = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
  
          Toast.makeText(EventListActivity.this, "onCreate\n"+kioskid, Toast.LENGTH_LONG).show();
          Toast.makeText(EventListActivity.this, "onCreate\n"+accesstoken, Toast.LENGTH_LONG).show();
  
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          // assign data from SharedPreferences 
          this.accesstoken = sharedPref.getString("accesstoken", null);
          this.kioskid = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
          Toast.makeText(EventListActivity.this, "onResume\n"+kioskid, Toast.LENGTH_LONG).show();
          Toast.makeText(EventListActivity.this, "onResume\n"+accesstoken, Toast.LENGTH_LONG).show();
  
  
          // loading the comments via AsyncTask
          new LoadEvents().execute();
      }
  
  
      /**
       * Retrieves recent Events from the server.
       */
      public void updateJSONdata() {
  
          arrayEventList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
          JSONParser jParser = new JSONParser();
  
          JSONObject jsonObj = jParser.getJSONFromUrl(EVENT_LIST_URL);
  
          try {
  
              jaEvents = jsonObj.getJSONArray(TAG_EVENT_LIST);
  
              // looping through all events according to the json object returned
              for (int i = 0; i &lt; jaEvents.length(); i++) {
                  JSONObject c = jaEvents.getJSONObject(i);
  
                  // gets the content of each tag
                  String eventID = c.getString(TAG_EVENT_ID);
                  String eventName = c.getString(TAG_EVENT_NAME);
  
                  // creating new HashMap
                  HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
                  map.put(TAG_EVENT_ID, eventID);
                  map.put(TAG_EVENT_NAME, eventName);
  
                  arrayEventList.add(map);
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
      }
  
      /**
       * Inserts the parsed data into the listview.
       */
      private void updateList() {
          ListAdapter adapter = new SimpleAdapter(this, arrayEventList,
                  R.layout.single_event_layout, new String[] { TAG_EVENT_ID, TAG_EVENT_NAME}, new int[] { R.id.event_id, R.id.event_name });
  
          setListAdapter(adapter);
  
          ListView lv = getListView();    
          lv.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
                  // do something
              }
          });
      }
  
      public class LoadEvents extends AsyncTask&lt;String, String, String&gt; {
  
          JSONParser jParser = new JSONParser();
  
          // check for success tag
          int status;
          String accesstoken ;
          String kioskid ;
  
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              pDialog = new ProgressDialog(EventListActivity.this);
              pDialog.setMessage("Loading Events...");
              pDialog.setIndeterminate(false);
              pDialog.setCancelable(true);
              pDialog.show();
          }
  
          @Override
          protected String doInBackground(String... arg0) {
  
  
              this.accesstoken = sharedPref.getString("accesstoken", null);
              this.kioskid = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
  
              try {
                  // building parameters for Logout
                  List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
                  params.add(new BasicNameValuePair("accesstoken", accesstoken));
                  params.add(new BasicNameValuePair("kioskid", kioskid));
                  Log.d("request!", "Starting");
  
                  // getting product details by HTTPRequest
                  JSONObject json = jParser.makeHttpRequest(EVENT_LIST_URL,"POST",params);
  
                  // checking your log for JSON response
                  Log.d("Loding Events Attemp", json.toString());
  
                  // assign status to JSON "status" tag
                  status = json.getInt(TAG_STATUS);
                  if (status == 1){
                      // print in console success message + JSON response message
                      Log.d("Events Loaded Successfully",json.toString());
  
  
                      updateJSONdata();
  
                      return json.getString(TAG_MESSAGE);
                  }else{
                      // print in console failure message + JSON response message
                      Log.d("Faild loding Events!", json.getString(TAG_MESSAGE));
  
                      return json.getString(TAG_MESSAGE);
  
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
              return null;
  
          }
  
          @Override
          protected void onPostExecute(String result) {
              super.onPostExecute(result);
              pDialog.dismiss();
              updateList();
          }
      }
  }
  </code></pre>
  
  <p><em><strong>Update 3</em></strong></p>
  
  <p><strong>JSONParser Class</strong></p>
  
  <pre><code>public class JSONParser {
      static InputStream is = null;
      static JSONObject jObj = null;
      static String json = "";
  
      // constructor
      public JSONParser() {
  
      }
  
      public JSONObject getJSONFromUrl(String url) {
          // making HTTP Request
          try {
              // Construct the client and the HTTP request.
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(url);
  
              // Execute the POST request and store the response locally.
              HttpResponse httpResponse = httpClient.execute(httpPost);
  
              // Extract data from the response.
              HttpEntity httpEntity = httpResponse.getEntity();
  
              // Open an inputStream with the data content.
              is = httpEntity.getContent();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          try {
              // Create a BufferedReader to parse through the inputStream.
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "iso-8859-1"), 8);
  
              // Declare a string builder to help with the parsing.
              StringBuilder sb = new StringBuilder();
              // Declare a string to store the JSON object data in string form.
              String line = null;
  
              // Build the string until null.
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
  
              // Close the input stream.
              is.close();
              // Convert the string builder data to an actual string.
              json = sb.toString();
          } catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
  
          // try to parse the string to a JSON Object
          try {
              jObj = new JSONObject(json);
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error Parsing data" + e.toString());
          }
  
          // return JSON String
          return jObj;
      }
  
      // function get json from url
      // by making HTTP POST or GET mehtod
      public JSONObject makeHttpRequest(String loginUrl, String method,
              List&lt;NameValuePair&gt; params) {
          // making HTTP Request
          try {
              // check for request method
              if(method == "POST"){
                  DefaultHttpClient httpClient = new DefaultHttpClient();
                  HttpPost httpPost = new HttpPost(loginUrl);
                  httpPost.setEntity(new UrlEncodedFormEntity(params));
                  HttpResponse httpResponse = httpClient.execute(httpPost);
  
                  HttpEntity httpEntity = httpResponse.getEntity();
                  is = httpEntity.getContent();
              }else 
                  // check for request method
                  if(method == "GET"){
                      DefaultHttpClient httpClient = new DefaultHttpClient();
                      String paramString = URLEncodedUtils.format(params, "utf-8");
                      loginUrl += "?" + paramString;
                      HttpGet httpGet = new HttpGet(loginUrl);
                      HttpResponse httpResponse = httpClient.execute(httpGet);
                      HttpEntity httpEntity = httpResponse.getEntity();
                      is = httpEntity.getContent();
              }
  
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          try {
                  BufferedReader  reader = new BufferedReader(
                      new InputStreamReader(is, "iso-8859-1"),8);
                  StringBuilder sb = new StringBuilder();
                  String  line = null;
  
                  while((line = reader.readLine()) != null){
                      sb.append(line + "\n");
                  }
                  is.close();
                  json = sb.toString();
              } catch (IOException e) {
                  Log.d("Buffer Error","Error Converting Reesult "+e.toString());
              }
  
          // try parse the string to JSON Object
          try {
              jObj = new JSONObject(json);
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error Parsing data" + e.toString());
          }
  
          // return JSON String
          return jObj;
      }
  
  }
  </code></pre>
  
  <p><strong>single_event_layout.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/gradient"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="5dp"
      android:background="@drawable/post_border_style"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout
          android:id="@+id/box"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="2dp"
          android:background="@drawable/post_background_style"
          android:orientation="horizontal" &gt;
  
          &lt;ImageView
              android:id="@+id/imageView1"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:src="@drawable/tapway_event_logo" /&gt;
  
          &lt;LinearLayout
              android:id="@+id/box"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="2dp"
              android:orientation="vertical"
              android:padding="5dp" &gt;
  
              &lt;TextView
                  android:id="@+id/event_id"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="bottom"
                  android:paddingBottom="2dip"
                  android:paddingLeft="5dp"
                  android:paddingTop="6dip"
                  android:textColor="#333"
                  android:textSize="16sp"
                  android:textStyle="bold" /&gt;
  
              &lt;TextView
                  android:id="@+id/event_name"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:paddingBottom="2dip"
                  android:paddingLeft="8dp"
                  android:textColor="#888" &gt;
              &lt;/TextView&gt;
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>event_list_layout.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/gradient" &gt;
  
      &lt;LinearLayout
          android:id="@+id/top_layover"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:background="@drawable/blue_gradient"
          android:orientation="horizontal" &gt;
  
          &lt;TextView
              style="@style/BlackText"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:gravity="center"
              android:text="@string/event_list"
              android:textAppearance="?android:attr/textAppearanceLarge" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;ListView
          android:id="@android:id/list"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_below="@+id/top_layover"
          android:background="#fff"
          android:divider="@android:color/transparent"
          android:scrollbars="none" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>