Issue
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();
}
}
}
}
Solution
If I remove/comment most of the Log.d statements it will run fine
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.
Answered By - aga
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.