Reputation: 69
I get this error whenever I press the login button in the app. Any suggestions?
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.android.login.LoginActivity.isEmpty(LoginActivity.java:93) at com.example.android.login.LoginActivity.access$000(LoginActivity.java:20) at com.example.android.login.LoginActivity$1.onClick(LoginActivity.java:47) at android.view.View.performClick(View.java:5191) at android.view.View$PerformClick.run(View.java:20916) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5972) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Here is my code:
package com.example.android.login;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;
/**
* Created by alex on 6/22/2015.
*/
public class LoginActivity extends Activity {
protected EditText usernameView;
protected EditText passwordView;
protected Button login;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
// Set up the login form.
usernameView = (EditText) findViewById(R.id.email);
passwordView = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.loginSubmit);
// Set up the submit button click handler
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Validate the log in data
boolean validationError = false;
StringBuilder validationErrorMessage =
new StringBuilder(getResources().getString(R.string.error_intro));
if (isEmpty(usernameView)) {
validationError = true;
validationErrorMessage.append(getResources().getString(R.string.error_blank_username));
}
if (isEmpty(passwordView)) {
if (validationError) {
validationErrorMessage.append(getResources().getString(R.string.error_join));
}
validationError = true;
validationErrorMessage.append(getResources().getString(R.string.error_blank_password));
}
validationErrorMessage.append(getResources().getString(R.string.error_end));
// If there is a validation error, display the error
if (validationError) {
Toast.makeText(LoginActivity.this, validationErrorMessage.toString(), Toast.LENGTH_LONG)
.show();
return;
}
// Set up a progress dialog
final ProgressDialog dlg = new ProgressDialog(LoginActivity.this);
dlg.setTitle("Please wait.");
dlg.setMessage("Logging in. Please wait.");
dlg.show();
String email=usernameView.getText().toString();
String password = passwordView.getText().toString();
Parse.initialize(LoginActivity.this, "rk1rRk43ArNaSp6kxrjwLOlLbwh2n1rDlWxUYB5p", "1Yd2vjpZLq7ofOoQoissgNJ4S6YtFO67ho59QYGT");
// Call the Parse login method
ParseUser.logInInBackground(email, password, new LogInCallback() {
@Override
public void done(ParseUser user, ParseException e) {
dlg.dismiss();
if (e != null) {
// Show the error message
Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
} else {
// Start an intent for the dispatch activity
Intent intent = new Intent(LoginActivity.this, DispatchActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
});
}
});
}
private boolean isEmpty(EditText etText) {
if (etText.getText().toString().trim().length() > 0) {
return false;
} else {
return true;
}
}
}
Upvotes: 0
Views: 590
Reputation: 141
I'm going to guess your usernameView
or passwordView
is null, which means it is probably not finding those views. I'd do a null check and have it print an error if it null, then see what happens.
EDIT: In light of your edited console, I can confirm it is, in fact, getting null for those edit views. You should first check for the underlying issues behind why this might be null. Generally speaking you CAN cast null to anything.
I suspect the ids you have are present in other layouts, but not in your R.layout.login_activity
. This will cause you to get null on these views, even if your IDE will not indicate this as an error (since the id technically exists, just not in your layout).
EDIT2: Further edit... This isn't really an issue of invoking a virtual method, it's more about calling a method on a null reference that is technically of that object (due to the cast). Null just means that there isn't really an object of that type, so it is not an instance of said object. So, you cannot call instantiated methods (non-static methods) on null.
I hope that is informational about the error you are facing! :)
Upvotes: 0