NullPointerException Crash-Collection of common programming errors
I’m getting a crash with a nullpointer expection. I’m getting the suer to sign up and I’m not sure if it’s my backend that’s failing. I’ve pointed my app to my php file and I’ve checked my Apache log and it is getting there.
code:
signUpButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0)
{
if (usernameText.length() > 0 &&
passwordText.length() > 0 &&
passwordAgainText.length() > 0 &&
eMailText.length() > 0
)
{
//TODO check email adress is valid
if (passwordText.getText().toString().equals(passwordAgainText.getText().toString())){
if (usernameText.length() >= 5 && passwordText.length() >= 5) {
Thread thread = new Thread(){
String result = new String();
@Override
public void run() {
result = imService.signUpUser(usernameText.getText().toString(),
passwordText.getText().toString(),
eMailText.getText().toString());
handler.post(new Runnable(){
public void run() {
if (result.equals(SERVER_RES_RES_SIGN_UP_SUCCESFULL)) {
showDialog(SIGN_UP_SUCCESSFULL);
}
else if (result.equals(SERVER_RES_SIGN_UP_USERNAME_CRASHED)){
showDialog(SIGN_UP_USERNAME_CRASHED);
}
else if (result.equals(SERVER_RES_SIGN_UP_FAILED))
{
showDialog(SIGN_UP_FAILED);
}
}
});
}
};
thread.start();
}
else{
showDialog(USERNAME_AND_PASSWORD_LENGTH_SHORT);
}
}
else {
showDialog(TYPE_SAME_PASSWORD_IN_PASSWORD_FIELDS);
}
}
else {
showDialog(FILL_ALL_FIELDS);
}
}
});
cancelButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Sorry! Please try again.", Toast.LENGTH_SHORT).show();
{
}
}});
}
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case TYPE_SAME_PASSWORD_IN_PASSWORD_FIELDS:
return new AlertDialog.Builder(SignUp.this)
.setMessage(R.string.signup_type_same_password_in_password_fields)
.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.create();
case FILL_ALL_FIELDS:
return new AlertDialog.Builder(SignUp.this)
.setMessage(R.string.signup_fill_all_fields)
.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.create();
case SIGN_UP_FAILED:
return new AlertDialog.Builder(SignUp.this)
.setMessage(R.string.signup_failed)
.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.create();
case SIGN_UP_USERNAME_CRASHED:
return new AlertDialog.Builder(SignUp.this)
.setMessage(R.string.signup_username_crashed)
.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.create();
case SIGN_UP_SUCCESSFULL:
return new AlertDialog.Builder(SignUp.this)
.setMessage(R.string.signup_successfull)
.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
})
.create();
case USERNAME_AND_PASSWORD_LENGTH_SHORT:
return new AlertDialog.Builder(SignUp.this)
.setMessage(R.string.username_and_password_length_short)
.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.create();
default:
return null;
}
}
@Override
protected void onResume() {
bindService(new Intent(SignUp.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);
super.onResume();
}
@Override
protected void onPause()
{
unbindService(mConnection);
super.onPause();
}
}
LogCat: 05-21 13:47:22.237: E/AndroidRuntime(522): FATAL EXCEPTION: main
05-21 13:47:22.237: E/AndroidRuntime(522): java.lang.NullPointerException
05-21 13:47:22.237: E/AndroidRuntime(522): at com.mekya.SignUp$2$1$1.run(SignUp.java:113)
05-21 13:47:22.237: E/AndroidRuntime(522): at android.os.Handler.handleCallback(Handler.java:587)
05-21 13:47:22.237: E/AndroidRuntime(522): at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 13:47:22.237: E/AndroidRuntime(522): at android.os.Looper.loop(Looper.java:123) 05-21 13:47:22.237: E/AndroidRuntime(522): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-21 13:47:22.237: E/AndroidRuntime(522): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 13:47:22.237: E/AndroidRuntime(522): at java.lang.reflect.Method.invoke(Method.java:521)
05-21 13:47:22.237: E/AndroidRuntime(522): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-21 13:47:22.237: E/AndroidRuntime(522): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-21 13:47:22.237: E/AndroidRuntime(522): at dalvik.system.NativeStart.main(Native Method)