I am building an app that needs to connect to a PHP API on a websever to get user credentials from a mySQL database. The API will return a JSON object as a result of the login.
I followed a tutorial online and it built everything on the main thread so I am trying to do Async Task. However I can’t get it to work and I keep getting a NullPointerException. The app spends about 5 minutes attempt to connect to my WAMP server and then just shuts down. Does it take that long to connect to servers to simulate poor network conditions?
The code is here:
private class ProcessLogin extends AsyncTask<Void, String, JSONObject> {
@Override
protected JSONObject doInBackground(Void... urls) {
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
publishProgress("Attempting to authenticate");
JSONObject json = userFunction.loginUser(email, password);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
publishProgress("Response received");
try {
if (json.getString(KEY_SUCCESS) != null) {
publishProgress("correct username/password");
}
else {
publishProgress("Incorrect username/password");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see android.os.AsyncTask#onProgressUpdate(Progress[])
*/
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
loginErrorMsg = (TextView) findViewById(R.id.login_error);
loginErrorMsg.setText(values[0]);
}
}
// Login button Click Event
public void beginLogin(View view) {
new ProcessLogin().execute();
}
My log cat error is here:
08-29 20:19:50.462: E/AndroidRuntime(1287): FATAL EXCEPTION: main
08-29 20:19:50.462: E/AndroidRuntime(1287): java.lang.NullPointerException
08-29 20:19:50.462: E/AndroidRuntime(1287): at com.example.onepass.LoginActivity$ProcessLogin.onPostExecute(LoginActivity.java:75)
08-29 20:19:50.462: E/AndroidRuntime(1287): at com.example.onepass.LoginActivity$ProcessLogin.onPostExecute(LoginActivity.java:1)
08-29 20:19:50.462: E/AndroidRuntime(1287): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-29 20:19:50.462: E/AndroidRuntime(1287): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-29 20:19:50.462: E/AndroidRuntime(1287): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-29 20:19:50.462: E/AndroidRuntime(1287): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 20:19:50.462: E/AndroidRuntime(1287): at android.os.Looper.loop(Looper.java:137)
08-29 20:19:50.462: E/AndroidRuntime(1287): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-29 20:19:50.462: E/AndroidRuntime(1287): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 20:19:50.462: E/AndroidRuntime(1287): at java.lang.reflect.Method.invoke(Method.java:511)
08-29 20:19:50.462: E/AndroidRuntime(1287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-29 20:19:50.462: E/AndroidRuntime(1287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-29 20:19:50.462: E/AndroidRuntime(1287): at dalvik.system.NativeStart.main(Native Method)
I have just started coding with Android SDK so if the error is really obvious, I apologize and appreciate your pointers.
Thanks
Error lies in line 75 of LoginActivity.java as stack track tells:
Since you did not provide line numbering, my blind guess is your json variable passed to onPostExecute() is simply
null. As you assume it is always valid (which is in 99% cases wrong assumption), you try to call getString() onnulled object, hence the NPE.Workaround is to check if
jsonis not null prior using it, which is always a good habit