-1

I'm using firebase in my android project. But this is what get when user trying to sign up in firebase using my application.

My errors log

09-15 13:54:23.992 2716-2716/com.example.err0r1096.tikteck E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.err0r1096.tikteck, PID: 2716

java.lang.IllegalStateException: Could not execute method for android:onClick
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
     at android.view.View.performClick(View.java:5637)
     at android.view.View$PerformClick.run(View.java:22429)
     at android.os.Handler.handleCallback(Handler.java:751)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:154)
     at android.app.ActivityThread.main(ActivityThread.java:6119)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  Caused by: java.lang.reflect.InvocationTargetException
     at java.lang.reflect.Method.invoke(Native Method)
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
     at android.view.View.performClick(View.java:5637) 
     at android.view.View$PerformClick.run(View.java:22429) 
     at android.os.Handler.handleCallback(Handler.java:751) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:154) 
     at android.app.ActivityThread.main(ActivityThread.java:6119) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
  Caused by: kotlin.KotlinNullPointerException
     at com.example.err0r1096.tikteck.Login.LoginToFireBase(Login.kt:26)
     at com.example.err0r1096.tikteck.Login.OneC(Login.kt:20)
     at java.lang.reflect.Method.invoke(Native Method) 
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
     at android.view.View.performClick(View.java:5637) 
     at android.view.View$PerformClick.run(View.java:22429) 
     at android.os.Handler.handleCallback(Handler.java:751) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:154) 
     at android.app.ActivityThread.main(ActivityThread.java:6119) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

and this my code :

package com.example.err0r1096.tikteck

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_login.*


class Login : AppCompatActivity() {

    private val mAuth: FirebaseAuth? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
    }
    fun OneC(view: View){

        LoginToFireBase(etEmail.text.toString(),etPassword.text.toString())

    }

    fun LoginToFireBase(email:String,password:String){

        mAuth!!.createUserWithEmailAndPassword(email,password)
                .addOnCompleteListener(this){ task ->

                    if (task.isSuccessful){
                        Toast.makeText(applicationContext,"Successful login",Toast.LENGTH_LONG).show()
}
                else
                {
                    Toast.makeText(this,"Login UnSuccessful",Toast.LENGTH_LONG).show()
                }
        }
    }
}

My application is crashing when user click on start button .

I think the problem is in my code and not on the firebase.

Adam S
  • 14,954
  • 6
  • 51
  • 79
SHAHIN a
  • 1
  • 1
  • 1
    Your code is throwing a `NullPointerException` on `LoginToFireBase(Login.kt:26)`. Did you initialize `mAuth` anywhere? If not, the [`mAuth!!` will throw a `NullPointerException`](https://stackoverflow.com/questions/34342413/what-is-the-kotlin-double-bang). – Frank van Puffelen Sep 15 '17 at 10:36
  • "mAuth!!" - try to never use it, if you're not 100% sure it's not null. Also avoid nullable objects as much as possible. This will lead to much cleaner and crash-free code. – Pavlo Zin Sep 15 '17 at 11:13

2 Answers2

0

You can check the official doc:

Kotlin's type system is aimed to eliminate NullPointerException's from our code. The only possible causes of NPE's may be:

  • An explicit call to throw NullPointerException();

  • Usage of the !! operator that is described below;

  • External Java code has caused it;

  • There's some data inconsistency with regard to initialization (an uninitialized this available in a constructor is used somewhere).

Using: b!! it will return a non-null value of b or throw an NPE if b is null

Check your code (you are using kotlin as java)

mAuth!!.createUserWithEmailAndPassword(email,password)

Did you initialize mAuth anywhere? If not, the mAuth!! will throw a NullPointerException

Community
  • 1
  • 1
Gabriele Mariotti
  • 192,671
  • 57
  • 469
  • 489
0

Try declaring the widgets by,

fun onC (view: View) {
val emailTxt = findViewById<View>(R.id.etEmail) as EditText
val passwordTxt = findViewById<View>(R.id.etPassword) as EditText
val email = emailTxt.text.toString.trim()
val password = passwordTxt.text.toString.trim()
LoginToFireBase (email, password)
}

This may resolve your problem :)

Ashwin
  • 81
  • 3
  • 10