Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialShane McC
3,005 PointsConnect To External Database Android Caused by: java.lang.NullPointerException
I'm in the process of building my first android app and my goal of this app is to connect to an external database. The syntax below is giving me a java.lang.NullPointerException error message. Below is all of my syntax and logcat information. What am I doing wrong?
MainActivity
package com.example.connecttoexternaldatabase;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.support.v7.app.ActionBarActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private TextView responseTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
responseTextView = (TextView) findViewById(R.id.responseTextView);
new GetAllCustomerTask().execute(new ApiConnector());
}
public void setTextToTextView(JSONArray jsonArray){
String theTextToDisplayOnPhone = "";
for(int i=0; i<jsonArray.length(); i++){
JSONObject json = null;
try{
json = jsonArray.getJSONObject(i);
theTextToDisplayOnPhone = theTextToDisplayOnPhone +
"Name : " +json.getString("FirstName")+" "+ json.get("LastName")+"\n"+
"Age : "+json.getInt("Age")+"\n"+
"Mobile Using : "+json.getString("Mobile")+"\n\n";
} catch (JSONException e){
e.printStackTrace();
}
}
responseTextView.setText(theTextToDisplayOnPhone);
}
private class GetAllCustomerTask extends AsyncTask<ApiConnector, Long, JSONArray>{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
// is it executed on the background thread
return params[0].GetAllCustomers();
}
@Override
protected void onPostExecute(JSONArray jsonArray){
// it is executed on the main thread
setTextToTextView(jsonArray);
}
}
}
ApiConnector
package com.example.connecttoexternaldatabase;
import java.io.IOException;
import java.net.HttpURLConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
public class ApiConnector {
public JSONArray GetAllCustomers(){
String url = "http://localhost/app/getAllCustomers.php";
HttpEntity httpEntity = null;
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
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;
}
}
Logcat Information
01-03 13:59:19.599: E/AndroidRuntime(762): FATAL EXCEPTION: main
01-03 13:59:19.599: E/AndroidRuntime(762): java.lang.NullPointerException
01-03 13:59:19.599: E/AndroidRuntime(762): at com.example.connecttoexternaldatabase.MainActivity.setTextToTextView(MainActivity.java:31)
01-03 13:59:19.599: E/AndroidRuntime(762): at com.example.connecttoexternaldatabase.MainActivity$GetAllCustomerTask.onPostExecute(MainActivity.java:58)
01-03 13:59:19.599: E/AndroidRuntime(762): at com.example.connecttoexternaldatabase.MainActivity$GetAllCustomerTask.onPostExecute(MainActivity.java:1)
01-03 13:59:19.599: E/AndroidRuntime(762): at android.os.AsyncTask.finish(AsyncTask.java:417)
01-03 13:59:19.599: E/AndroidRuntime(762): at android.os.AsyncTask.access$300(AsyncTask.java:127)
01-03 13:59:19.599: E/AndroidRuntime(762): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
01-03 13:59:19.599: E/AndroidRuntime(762): at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 13:59:19.599: E/AndroidRuntime(762): at android.os.Looper.loop(Looper.java:130)
01-03 13:59:19.599: E/AndroidRuntime(762): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-03 13:59:19.599: E/AndroidRuntime(762): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 13:59:19.599: E/AndroidRuntime(762): at java.lang.reflect.Method.invoke(Method.java:507)
01-03 13:59:19.599: E/AndroidRuntime(762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:870)
01-03 13:59:19.599: E/AndroidRuntime(762): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
01-03 13:59:19.599: E/AndroidRuntime(762): at dalvik.system.NativeStart.main(Native Method)