title: Android, Connecting to MySQL using PHP: Null Pointer exception
body: <p>Im a newbie to android, I am learning to connect to a server through android client using Php, MySql and JSON. For testing purpose im running on localhost.
  So for here's what I've done.
  <strong>Database demo.php</strong></p>
  
  <pre><code>public class Database_demo extends ListActivity {
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
  
      String result = null;
      InputStream is = null;
      StringBuilder sb = null;
      ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
      List&lt;String&gt; r = new ArrayList&lt;String&gt;();
  
      try{
  
      //http post
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost("http://10.0.2.2/PhpAndMySql/category.php");
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      HttpResponse response = httpclient.execute(httppost);
      HttpEntity entity = response.getEntity();
      is = entity.getContent();
      }
      catch(Exception e){
          Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
     }
  
      //Convert response to string  
      try
      {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
  
        sb = new StringBuilder();
  
        String line = null;
  
        while ((line = reader.readLine()) != null) 
        {
           sb.append(line + "\n");
        }
  
        is.close();
  
        result = sb.toString();
      }
      catch(Exception e)
      {
          Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
      }
      //END Convert response to string   
      try{
              JSONArray jArray = new JSONArray(result);
              JSONObject json_data=null;
              for(int i=0;i&lt;jArray.length();i++)
              {
                 json_data = jArray.getJSONObject(i);
                 r.add(json_data.getString("category"));
             }
             setListAdapter(new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_expandable_list_item_1, r));
          }
          catch(JSONException e1){
              Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
          } catch (ParseException e1) {
              Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>category.php</strong></p>
  
  <pre><code>&lt;?php
  mysql_connect("localhost","root","");
  mysql_select_db("test");
  $q=mysql_query("SELECT * FROM category ORDER BY 'category'.'category' ASC");
  while($row=mysql_fetch_assoc($sql))
  $output[]=$row;
  print(json_encode($output));
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p><strong>MySQL</strong></p>
  
  <pre><code>CREATE TABLE `test`.`category` (
  `category_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `category` VARCHAR( 255 ) NOT NULL
   ) ENGINE = MYISAM ;
  </code></pre>
  
  <p>I am getting a NullPointer Exception, when I execute in android.
  Is the Php File correct?</p>
  
  <p>Please I need your help with this!
  Thanks</p>