title: android connecting to mysql using php
body: <p>I am trying to get a variable from my database and I am getting force close:</p>
  
  <pre><code>    11-16 16:33:41.757: E/AndroidRuntime(26305): FATAL EXCEPTION: main
  11-16 16:33:41.757: E/AndroidRuntime(26305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databaseexample/com.example.databaseexample.MainActivity}: java.lang.NullPointerException
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.os.Handler.dispatchMessage(Handler.java:99)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.os.Looper.loop(Looper.java:130)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.main(ActivityThread.java:3687)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at java.lang.reflect.Method.invokeNative(Native Method)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at java.lang.reflect.Method.invoke(Method.java:507)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at dalvik.system.NativeStart.main(Native Method)
  11-16 16:33:41.757: E/AndroidRuntime(26305): Caused by: java.lang.NullPointerException
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONTokener.nextValue(JSONTokener.java:90)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONArray.&lt;init&gt;(JSONArray.java:87)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONArray.&lt;init&gt;(JSONArray.java:103)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at com.example.databaseexample.MainActivity.onCreate(MainActivity.java:100)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
  11-16 16:33:41.757: E/AndroidRuntime(26305):    ... 11 more
  </code></pre>
  
  <p><strong>my android part:</strong></p>
  
  <pre><code>public class MainActivity extends ListActivity {
  
      private List nameValuePairs;
      private JSONArray jArray;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          String result = null;
  
           InputStream is = null;
  
           StringBuilder sb=null;
  
           String result1=null;
  
           //http post
  
           try{
  
           HttpClient httpclient = new DefaultHttpClient();
  
           HttpPost httppost = new HttpPost("http://lab.sif.mruni.eu/~sanevys/parduotuve/puslapiai/android.php");
  
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
           HttpResponse response = httpclient.execute(httppost);
  
           HttpEntity entity = response.getEntity();
  
           is = entity.getContent();
  
           }catch(Exception e){
  
           Log.e("log_tag", "Error in http connection"+e.toString());
  
           }
  
           //convert response to string
  
           try{
  
           BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  
           sb = new StringBuilder();
  
           sb.append(reader.readLine() + "\n");
  
           String line="";
  
           while ((line = reader.readLine()) != null) {
  
           sb.append(line + "\n");
  
           }
  
           is.close();
  
           result1=sb.toString();
  
           }catch(Exception e){
  
           Log.e("log_tag", "Error converting result "+e.toString());
  
           }
  
           //paring data
  
           int fd_id;
  
           String fd_name;
  
           try{
  
           jArray = new JSONArray(result);
  
           JSONObject json_data=null;
  
           for(int i=0;i&lt;jArray.length();i++){
  
           json_data = jArray.getJSONObject(i);
  
           fd_id=json_data.getInt("FOOD_ID");
  
           fd_name=json_data.getString("FOOD_NAME");
  
           }
  
           }catch(JSONException e){
  
           Toast.makeText(getBaseContext(), "No Food Found", Toast.LENGTH_LONG).show();
  
           }catch (ParseException e){
  
           e.printStackTrace();
  
           }
  
           }
  
  }
  </code></pre>
  
  <p><strong>My php part:</strong></p>
  
  <pre><code>&lt;?php
  # FileName="Connection_php_mysql.htm"
  # Type="MYSQL"
  # HTTP="true"
  $hostname_lab = "localhost";
  $database_lab = "database";
  $username_lab = "username";
  $password_lab = "password";
  $lab = mysql_pconnect($hostname_lab, $username_lab, $password_lab) or trigger_error(mysql_error(),E_USER_ERROR); 
  mysql_select_db("database");
    $sql=mysql_query("select * from FOOD where FOOD_NAME like 'A%'");
    while($row=mysql_fetch_assoc($sql)) $output[]=$row;
    print(json_encode($output));
    mysql_close();
  ?&gt;
  </code></pre>
  
  <p><strong>My xml</strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello_world" /&gt;
  &lt;ListView         
      android:id="@android:id/list"
      android:layout_height="match_parent"
      android:layout_width="match_parent"/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Any help/advice will be appreciated :)</p>