0

I am trying to get a variable from my database and I am getting force close:

    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.<init>(JSONArray.java:87)
11-16 16:33:41.757: E/AndroidRuntime(26305):    at org.json.JSONArray.<init>(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

my android part:

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<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();

         }

         }

}

My php part:

<?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();
?>

My xml

<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
<ListView         
    android:id="@android:id/list"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>
</RelativeLayout>

Any help/advice will be appreciated :)

5
  • 1. did u tested the php service in broswer first? 2. Did u added internet permission? 3. is Main Activity java declared in manifest? Commented Nov 16, 2013 at 12:32
  • i think ur mistaken in giving id to listview ..check once Commented Nov 16, 2013 at 12:37
  • @ MT8 1. yes, broswer gives this: lab.sif.mruni.eu/~sanevys/parduotuve/puslapiai/android.php 2. yes, added 3. yes, declared Commented Nov 16, 2013 at 12:53
  • @MT8 I can't find, where I gave that id Commented Nov 16, 2013 at 12:54
  • Maybe I need to declare listview in xml? Commented Nov 16, 2013 at 12:57

1 Answer 1

1

Since you are extending your Activity from ListActivity your ListView id should be

 android:id="@android:id/list"

Check if you have given proper id in xml file

Or if you have not added a ListView to xml file, add a ListView with the above mentioned id. All the subclasses of ListActivity should have a ListView.

Sign up to request clarification or add additional context in comments.

3 Comments

Do I need to create new xml file? Or Edit MainActivity.xml file?
@whiteLT No need to create new xml file.activity_main.xml is enough. xml file seems to be proper. You are still getting error?
@whiteLT You have not initialized nameValuePairs it is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.