I was trying to create a simple registration app in Android Studio using Java Language.
But I am getting this typical errors cannot find symbol and Variable 'email' might not have been initialized at line number 25 which is String email = email.getText().toString(); in MainActivity.java, even though I am sure that I have initialized all of my variables.
Below is my code of MainActivity.java:
public class MainActivity extends AppCompatActivity {
EditText firstname, lastname, email, password, repassword;
Button signup, signin;
DBHelper DB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstname = (EditText) findViewById(R.id.firstname);
lastname = (EditText) findViewById(R.id.lastname);
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
repassword = (EditText) findViewById(R.id.repassword);
signup = (Button) findViewById(R.id.btnsignup);
signin = (Button) findViewById(R.id.btnsignin);
DB = new DBHelper(this);
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String fname = firstname.getText().toString();
String lname = lastname.getText().toString();
String email = email.getText().toString();
String pass = password.getText().toString();
String repass = repassword.getText().toString();
if(email.equals("")||pass.equals("")||repass.equals(""))
Toast.makeText(MainActivity.this, "Please enter all the fields", Toast.LENGTH_SHORT).show();
else{
if(pass.equals(repass)){
Boolean checkuser = DB.checkEmail(email);
if(checkuser==false){
Boolean insert = DB.insertData(fname, lname, email, pass);
if(insert==true){
Toast.makeText(MainActivity.this, "Registered successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
startActivity(intent);
}else{
Toast.makeText(MainActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(MainActivity.this, "User already exists! please sign in", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this, "Passwords do not match", Toast.LENGTH_SHORT).show();
}
} }
});
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
}
});
}
}
Below is my code for activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First Name"
android:layout_marginTop="50dp"/>
<EditText
android:id="@+id/lastname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Last Name"
android:layout_marginTop="50dp"
android:layout_below="@id/firstname"/>
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:layout_marginTop="50dp"
android:layout_below="@id/lastname"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_marginTop="50dp"
android:layout_below="@+id/email"/>
<EditText
android:id="@+id/repassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Retype Password"
android:layout_marginTop="50dp"
android:layout_below="@+id/password"/>
<Button
android:id="@+id/btnsignup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:layout_marginTop="50dp"
android:layout_below="@+id/repassword"/>
<Button
android:id="@+id/btnsignin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Existing user! Go to Sign in page"
android:layout_marginTop="50dp"
android:layout_below="@+id/btnsignup"/>
</RelativeLayout>
Any help would be appreciated. I have been going through it a lot of time and reviewed all my variables and I think that I have initialized them before using them. I have searched over the internet and viewed many links, but all of those states that variables should be initialized before using them, and I have made it sure thrice that my variables are initialized. I am fed up with this error. Anyone please look into it and help me sort it out. Thanks in advanced.
>Solution :
Try to change the name of the variable to something other than email:
String mail = email.getText().toString();