0

Hi I'm new in the world of android. So I tested a tutorial do fetch data from a XAMPP database and list it in my app with listview. Everything is like in the tutorial, so why my app doesnt't work? When I start the app i got an error message and the app crushes.

Thank you for your help!

public class MainActivity extends ActionBarActivity {

private TextView responseTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    this.responseTextView = (TextView) this.findViewById(R.id.responseTextView);


    new GetAllCustomerTask().execute(new ApiConnector());


}

public void setTextToTextView(JSONArray jsonArray)
{
    String s  = "";
    for(int i=0; i<jsonArray.length();i++){

        JSONObject json = null;
        try {
            json = jsonArray.getJSONObject(i);
            s = s +
                    "ID : "+json.getInt("id")+"\n"+
                    "Name : "+json.getString("name")+"\n\n";
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    this.responseTextView.setText(s);

}


private class GetAllCustomerTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
    @Override
    protected JSONArray doInBackground(ApiConnector... params) {

        // it is executed on Background thread

        return params[0].GetAllCustomers();
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {

        setTextToTextView(jsonArray);


    }
}

}

public class ApiConnector {

public JSONArray GetAllCustomers()
{
    // URL for getting all customers


    String url = "http://localhost/getAllCustomers.php";

    // Get HttpResponse Object from url.
    // Get HttpEntity from Http Response Object

    HttpEntity httpEntity = null;

    try
    {

        DefaultHttpClient httpClient = new DefaultHttpClient();  // Default HttpClient
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);

        httpEntity = httpResponse.getEntity();



    } catch (ClientProtocolException e) {

        // Signals error in http protocol
        e.printStackTrace();

        //Log Errors Here



    } catch (IOException e) {
        e.printStackTrace();
    }


    // Convert HttpEntity into JSON Array
    JSONArray jsonArray = null;

    if (httpEntity != null) {
        try {
            String entityResponse = EntityUtils.toString(httpEntity);

            Log.e("Entity Response  : ", entityResponse);

            jsonArray = new JSONArray(entityResponse);

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return jsonArray;


}

}

This is my logcat

04-06 22:17:21.550  23914-23914/diplomarbeit.at.dbfetchcontenttester E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: diplomarbeit.at.dbfetchcontenttester, PID: 23914
java.lang.NullPointerException
        at diplomarbeit.at.dbfetchcontenttester.MainActivity.setTextToTextView(MainActivity.java:34)
        at diplomarbeit.at.dbfetchcontenttester.MainActivity$GetAllCustomerTask.onPostExecute(MainActivity.java:66)
        at diplomarbeit.at.dbfetchcontenttester.MainActivity$GetAllCustomerTask.onPostExecute(MainActivity.java:53)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5105)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)

2 Answers2

1

Your jsonArray object when executing setTextToTextView(JSONArray jsonArray) seems to be null. I recommend you to use the debugger from Eclipse / Android studio to see where it fails to get data in your public JSONArray GetAllCustomers() method. You need first to make sure that you get the data from your local server. If that's the case, maybe take a look at the json parsing.

I'm also assuming you are running this on an emulator and not on a mobile phone/tablet. You are using a localhost address which will only work if your local server is on the same machine as your "device".

Quanturium
  • 5,698
  • 2
  • 30
  • 36
  • I tested it with an emulator but the result is the same. When i debug the code the logcat tells me: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference but i can't solve the problem. why it has worked in the video of the tutorial - it was exactly the same code?! – android_noob Apr 06 '15 at 22:35
0

Your jsonArray has "id":"0" inside of it, because the number is in quotes ("0") I think its being seen as text/String as opposed to an integer:

Currently:

"ID : "+json.getInt("id")+"\n"+

Try:

"ID : "+json.getString("id")+"\n"+

If this doesn't make a difference try putting the URL into a web browser on the device you are testing it on to make sure its working on your test device/emulator.

Mark
  • 9,604
  • 5
  • 36
  • 64