0

I'm currently working on my first project, so I'm sorry if it sounds like a stupid question.

I'm having trouble with a piece of code that should attempt to create a Firebase acccount with the email and password provided in the text boxes in the UI when you click a button. Instead, whenever I click that button, the app crashes with the error: java.lang.IllegalArgumentException: Given String is empty or null.

Code:

    //Create new User
    var email = emailID.text.toString().trim()
    var pwd = passwordID.text.toString().trim()


    create_acc_button.setOnClickListener{

        mAuth!!.createUserWithEmailAndPassword(email, 
        pwd).addOnCompleteListener(this
        ) { task: Task<AuthResult> ->
            if (task.isSuccessful){
                var user:FirebaseUser = mAuth!!.currentUser!!
                Log.d("User", user.email.toString())
            }else{
                Log.d("Error:", task.toString())
            }
        }
    }

Logcat:

    java.lang.IllegalArgumentException: Given String is empty or null
        at com.google.android.gms.common.internal.zzbq.zzgm(Unknown Source:10)
        at com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(Unknown Source:0)
        at com.example.david.mykotlinfirebaseintro.MainActivity$onCreate$1.onClick(MainActivity.kt:37)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

2 Answers 2

2

In this way; email and pwd variables filled at first start. Your changes are not effect the variables.

You must move this lines into the clicklistener.

 //Create new User
var email = emailID.text.toString().trim()
var pwd = passwordID.text.toString().trim()
Sign up to request clarification or add additional context in comments.

2 Comments

That worked beautifully. Thank you! So the problem was that the text was changing, but it was too late for the variables to be updated?
Exactly! In this way, you get texts when the user clicks on the button.
0

surround code with condition in button click

if (email != null || !email.isEmpty()) {
     //your positive code
} else {
     //your negative code    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.