Hi I understand there are duplicates like (Firebase Registration, Given String is empty or null) and (I keep getting"java.lang.IllegalArgumentException: Given String is empty or null" and it has something to do with auth = FirebaseAuth.getInstance();").
Both articles mentioned solution about the api_key. I have checked mine and they were already exist so I strongly believe it is no longer due to that. As such I researched further and found out that I need to have something to check empty so I got these (Check whether a string is not null and not empty) and (Checking if a string is empty or null in Java).
I did tried their solutions but to no avail - I am still getting the above-mentioned error. I coded it using my instincts and the guides given but I am still not entirely sure if I did it right or not. As in, whether the mistakes are the codes itself or I placed them wrongly.
Can anyone help me to check and provide guidance so I can correct the mistake? NOTE: I'm using Firebase for authentication.
Below are my codes:
package com.example.run_h.boav2;
import android.app.ProgressDialog;
import android.content.Intent;
import android.hardware.usb.UsbRequest;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
public class MainActivity extends AppCompatActivity {
private EditText Username;
private EditText Password;
private TextView Info;
private Button Login;
private int counter = 3;
private Button userRegistration;
private FirebaseAuth firebaseAuth;
private ProgressDialog progressDialog;
private TextView forgotPassword;
String name, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Username = (EditText)findViewById(R.id.etUsername);
Password = (EditText)findViewById(R.id.etPassword);
Info = (TextView)findViewById(R.id.tvInfo);
Login = (Button)findViewById(R.id.btnLogin);
userRegistration = (Button)
findViewById(R.id.tvRegister);
forgotPassword =
(TextView)findViewById(R.id.tvForgotPassword);
Info.setText("# of Attempts Left: 3");
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
progressDialog = new ProgressDialog(this);
//user has logged in and never once logged out. auto
bring to the secondactivity (dashboard) page.
if(user != null){
finish();
startActivity(new Intent(MainActivity.this,
SecondActivity.class));
}
Login.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
validate(Username.getText().toString(),
Password.getText().toString());
}
});
userRegistration.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,
RegistrationActivity.class));
}
});
forgotPassword.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,
PasswordActivity.class ));
}
});
}
private void validate(String userName, String
userPassword){
progressDialog.setMessage("Please hold on. We are
testing your patience!");
progressDialog.setProgressStyle(progressDialog.STYLE_SPINNER);
progressDialog.show();
firebaseAuth.signInWithEmailAndPassword(userName,
userPassword).addOnCompleteListener(new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult>
task) {
if(task.isSuccessful()){
progressDialog.dismiss();
checkEmailVerification();
}
else{
//Toast.makeText(MainActivity.this, "Login failed.
Incorrect username or password. Please try again.",
Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
checkEmptyCredentials();
counter--;
Info.setText("# of Attempts Left: " + counter);
if(counter == 0){
Login.setEnabled(false);
startActivity(new Intent(MainActivity.this, PasswordActivity.class)); //forces user to reset password
}
}
}
});
}
private Boolean checkEmptyCredentials(){
Boolean result = false;
name = Username.getText().toString();
password = Password.getText().toString();
if(name != null && name.isEmpty() || password != null && password.isEmpty()){
Toast.makeText(this, "All fields must not be blank.", Toast.LENGTH_SHORT).show();
}else{
result = true;
}
return result;
}
private void checkEmailVerification(){
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
Boolean emailflag = firebaseUser.isEmailVerified();
if(emailflag){
finish();
startActivity(new Intent(MainActivity.this, Main2Activity.class));
}else{
Toast.makeText(this, "Please verify your email and account to login.", Toast.LENGTH_SHORT).show();
firebaseAuth.signOut();
}
}
}
Error trace
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.run_h.boav2, PID: 27092
java.lang.IllegalArgumentException: Given String is empty or null
at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source)
at com.example.run_h.boav2.MainActivity.validate(MainActivity.java:89)
at com.example.run_h.boav2.MainActivity.access$200(MainActivity.java:21)
at com.example.run_h.boav2.MainActivity$1.onClick(MainActivity.java:64)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10814)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)