Reputation: 3
I'm working on a login and register system and I'm getting this error while trying to tap the 'Login' button.
08-06 00:36:19.258 25808-25808/com.kylek.hatework E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.kylek.hatework, PID: 25808
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.kylek.hatework.Login.onClick(Login.java:42)
at android.view.View.performClick(View.java:5254)
at android.view.View$PerformClick.run(View.java:21173)
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:6837)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Here's my Login.java
package com.kylek.hatework;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends ActionBarActivity implements View.OnClickListener {
Button bLogin;
TextView registerLink;
EditText etUsername, etPassword;
String username, password;
UserLocalStore userLocalStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
bLogin = (Button) findViewById(R.id.bLogin);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
registerLink = (TextView) findViewById(R.id.tvRegisterLink);
bLogin.setOnClickListener(this);
registerLink.setOnClickListener(this);
userLocalStore = new UserLocalStore(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bLogin:
Log.v("username", etUsername.getText().toString());
Log.v("password", etPassword.getText().toString());
User user = new User(username, password);
authenticate(user);
break;
case R.id.tvRegisterLink:
Intent registerIntent = new Intent(Login.this, Register.class);
startActivity(registerIntent);
break;
}
}
private void authenticate(User user) {
ServerRequests serverRequest = new ServerRequests(this);
serverRequest.fetchUserDataAsyncTask(user, new GetUserCallback() {
@Override
public void done(User returnedUser) {
if (returnedUser == null) {
showErrorMessage();
} else {
logUserIn(returnedUser);
}
}
});
}
private void showErrorMessage() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Login.this);
dialogBuilder.setMessage("Incorrect user details");
dialogBuilder.setPositiveButton("Ok", null);
dialogBuilder.show();
}
private void logUserIn(User returnedUser) {
userLocalStore.storeUserData(returnedUser);
userLocalStore.setUserLoggedIn(true);
startActivity(new Intent(this, MainActivity.class));
}
}
I've looked around on a bunch of other articles on SOF, but nothing that helps me in this particular situation. Maybe I'm missing something easy, but I've had enough of trying to fix it for the night. Any suggestions?
Upvotes: 0
Views: 4579
Reputation: 1056
I've pointed out some suggestions in your code below. Kindly verify those :
public class Login extends ActionBarActivity implements View.OnClickListener {
Button bLogin;
TextView registerLink;
EditText etUsername, etPassword;
String username, password;
UserLocalStore userLocalStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
bLogin = (Button) findViewById(R.id.bLogin);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
registerLink = (TextView) findViewById(R.id.tvRegisterLink);
You are getting a NullPointerException on one of your EditText (etUsername here). It means that particular Edit Text has not been initialized and you are calling a method on it.
On this particular line below, you are getting the exception. You are calling getText() method on etUsername which is null
Log.v("username", etUsername.getText().toString());
Follow the steps below to identify your issue:
Verify if the above ids of EditText in your R.layout.activity_login xml file, exist or not.
Verify that if the ids of EditText referred inside findViewById(), belongs to your R.layout.activity_login layout file.
Inside the onClick() method, the userName and password variables are initialized with the default value as null.
You need to initialize them from the values you will be getting from the EditText(in case it's not null):
username = etUsername.getText().toString();
password = etPassword.getText().toString();
Once the variables are properly initialized, use them further:
User user = new User(username, password);
authenticate(user);
Upvotes: 2
Reputation: 1299
First make sure there is no any another views on your layout with same id`s like etUsername and etPassword, also you must init username and password variables; change your case ti this:
case R.id.bLogin:
username=etUsername.getText().toString();
password=etPassword.getText().toString();
Log.v("username", username);
Log.v("password", password);
User user = new User(username, password);
authenticate(user);
break;
Upvotes: 0
Reputation: 2202
The username and password Strings are used without being initialized.
In your onClickMethod
User user = new User(username, password);
authenticate(user);
you create an object with null strings and use it so it will give error.
Upvotes: 0