Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Good morning StackOver Flow Pros,
I need a little help here. I am running an Android application which calls out to a MySql database via a php script, When I run the application I get a Fatal Exception error for AsyncTask #1 caused by, a null pointer exception. It said it occurred while executing doInBackground. This is a tutorial I am following from MyBringBack. I really need to wrap my head around this because I have been trying to figure this out for days. If I remove/comment most of the Log.d statements it will run fine but I will not see the tags for what is going on from the JSON responses, the code is listed below, could anyone help me figure this out or tell me what is wrong with the code.
Here is the error log from logcat
10-25 09:27:47.818: E/JSON Parser(1041): Error parsing data org.json.JSONException: Value array(2) of type java.lang.String cannot be converted to JSONObject
10-25 09:27:47.818: W/dalvikvm(1041): threadid=11: thread exiting with uncaught exception (group=0xb1b06ba8)
10-25 09:27:47.828: E/AndroidRuntime(1041): FATAL EXCEPTION: AsyncTask #1
10-25 09:27:47.828: E/AndroidRuntime(1041): Process: com.bobcatservice, PID: 1041
10-25 09:27:47.828: E/AndroidRuntime(1041): java.lang.RuntimeException: An error occured while executing doInBackground()
10-25 09:27:47.828: E/AndroidRuntime(1041): at android.os.AsyncTask$3.done(AsyncTask.java:300)
10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
10-25 09:27:47.828: E/AndroidRuntime(1041): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-25 09:27:47.828: E/AndroidRuntime(1041): at java.lang.Thread.run(Thread.java:841)
10-25 09:27:47.828: E/AndroidRuntime(1041): Caused by: java.lang.NullPointerException
10-25 09:27:47.828: E/AndroidRuntime(1041): at com.bobcatservice.Login$AttemptLogin.doInBackground(Login.java:126)
10-25 09:27:47.828: E/AndroidRuntime(1041): at com.bobcatservice.Login$AttemptLogin.doInBackground(Login.java:1)
10-25 09:27:47.828: E/AndroidRuntime(1041): at android.os.AsyncTask$2.call(AsyncTask.java:288)
10-25 09:27:47.828: E/AndroidRuntime(1041): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-25 09:27:47.828: E/AndroidRuntime(1041): ... 4 more
10-25 09:27:49.968: E/WindowManager(1041): android.view.WindowLeaked: Activity com.bobcatservice.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b1e8e430 V.E..... R......D 0,0-684,192} that was originally added here
10-25 09:27:49.968: E/WindowManager(1041): at android.view.ViewRootImpl.(ViewRootImpl.java:348)
10-25 09:27:49.968: E/WindowManager(1041): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
10-25 09:27:49.968: E/WindowManager(1041): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-25 09:27:49.968: E/WindowManager(1041): at android.app.Dialog.show(Dialog.java:286)
And here is the code in question
//AsyncTask is a seperate thread than the thread that runs the GUI
//Any type of networking should be done with asynctask.
class AttemptLogin extends AsyncTask<String, String, String> {
* Before starting background thread Show Progress Dialog
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
try {
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, Welcome.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
} catch (JSONException e) {
e.printStackTrace();
return null;
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
It seems like we can find an issue in some of your log invocations in doInBackground()
method. What about this one?
Log.d("request!", "starting");
No, seems good. What about the next one?
Log.d("Login attempt", json.toString());
Aha! How do we get this json
variable?
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
Check the result of your http request, you probably get null
here.
What if json
is not null? Well, we have another suspicious log call:
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
As far as I remember, if the second argument of Log.d()
is null
, you're getting NullPointerException
, so check this one too.
What if it isn't an issue either? Well, I suggest you use debugger here.
–
–
on top of ur code pls check create_product.php exists (here in android folder) and replace table_name with your table name and make $response["message"] = mysql_error(); to know what is wrong with create_product.php in logcat of android sdk
// url to create send data. This contains the ip address of my machine on which the local server is running. You will write the IP address of your machine
private static String url = "http://192.168.***.1:8080/android/create_product.php";
create_product.php
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['id']) && isset($_POST['name'])) {
$id = $_POST['id'];
$name = $_POST['name'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO table_name(id, name) VALUES('$id', '$name')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = mysql_error();;
// echoing JSON response
echo json_encode($response);
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
And also add in manifest this code else this error will generate
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.