Unable to instantiate activity | Caused by: kotlin.KotlinNullPointerException - android

I'm getting this error
FATAL EXCEPTION: main
Process: com.demo.gandharv.exkotlin, PID: 7742
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.pantelwar.gandharv.scobokotlin/com.demo.gandharv.exkotlin.RegisterActivity}: kotlin.KotlinNullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3095)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3351)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7230)
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)
Caused by: kotlin.KotlinNullPointerException
at com.demo.gandharv.exkotlin.RegisterActivity.(RegisterActivity.kt:12)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3085)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3351) 
at android.app.ActivityThread.access$1100(ActivityThread.java:222) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1796) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7230) 
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) 
I have defined the RegisterActivity in my AndroidMenifest.xml.
I have no idea why i'm getting this error.
Here's my LoginActivity.kt
package com.demo.gandharv.exkotlin
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_login.*
class LoginActivity : AppCompatActivity() {
var email: String = ""
var password: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
loginButton.setOnClickListener {
email = emailEditText.text.toString()
password = passwordEditText.text.toString()
}
registerTextView.setOnClickListener {
val intent = Intent(this#LoginActivity, RegisterActivity::class.java)
startActivity(intent)
}
}
}

Related

Screen empty after rotation

I need to handle screen rotation so I read about saved state and I tried to implement it bit I cant !
here is the fragment screen :
#AndroidEntryPoint
class ImagePickerFragment : Fragment() {
private lateinit var imagePickerBinding: FragmentImagePickerBinding
private val imagePickerViewModel: ImagePickerViewModel by viewModels {
SavedStateViewModelFactory(activity?.applicationContext as Application, this)
}
private lateinit var imagePickerAdapter: ImagePickerAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
imagePickerBinding = FragmentImagePickerBinding.inflate(inflater, container, false)
imagePickerBinding.lifecycleOwner = this
observeImageList()
observeSearchText()
observeImageClicked()
return imagePickerBinding.root
}
override fun onSaveInstanceState(outState: Bundle) {
imagePickerViewModel.trigger()
super.onSaveInstanceState(outState)
}
private fun observeImageList() {
imagePickerViewModel.observeImageListData.observe(
viewLifecycleOwner,
EventObserver { imageList ->
initializeImageList(imageList)
})
}
So here I should initialize viewModel with savedstate but this getting error :
09-23 13:10:38.621 26928-26928/com.payback.imagepicker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.payback.imagepicker, PID: 26928
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.payback.imagepicker/com.payback.imagepicker.manager.base.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.payback.imagepicker.presentation.ui.image_picker.ImagePickerViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.payback.imagepicker.presentation.ui.image_picker.ImagePickerViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
at com.payback.imagepicker.presentation.ui.image_picker.ImagePickerFragment.getImagePickerViewModel(ImagePickerFragment.kt:30)
at com.payback.imagepicker.presentation.ui.image_picker.ImagePickerFragment.observeImageList(ImagePickerFragment.kt:73)
at com.payback.imagepicker.presentation.ui.image_picker.ImagePickerFragment.onCreateView(ImagePickerFragment.kt:45)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:3065)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2988)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.InstantiationException: java.lang.Class<com.payback.imagepicker.presentation.ui.image_picker.ImagePickerViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFact
09-23 13:15:38.690 26928-26928/com.payback.imagepicker I/Process: Sending signal. PID: 26928 SIG: 9
And this is My view model :
#HiltViewModel
class ImagePickerViewModel #Inject constructor(
private val imageListUseCase: ImageListUseCase,
private val responseManager: ResponseManager,
private val savedStateHandle: SavedStateHandle
) : BaseViewModel() {
private val _observeImageListData :
MutableLiveData<Event<ArrayList<Image>>> = savedStateHandle.getLiveData("key")
fun trigger() {
savedStateHandle.set("key", _observeImageClicked.value)
}
Please can any one help me ? I cant understand it well or implement it right so what's wrong?

Instantation Error when running Android studio App with OkHTTTP Using Kotlin

using OkHTTP I am trying to retrieve a text file and put hat into the textView on the main activity,
Gradle will say build successful but the emulator app will just crash on launch and provide this error.
I have added the correct permissions in the AndroidManifest.xml and the correct implementation in the gradle build
package com.example.vectortestapi
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import okhttp3.*
import java.io.IOException
abstract class MainActivity : AppCompatActivity() {
private val mTextView: TextView = findViewById<TextView>(R.id.resultText);
private val client = OkHttpClient()
private val url = "https://publicobject.com/helloworld.txt"
private val request: Request = Request.Builder()
.url(url)
.build();
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getAPI();
}
private fun getAPI(){
client.newCall(request).enqueue(object: Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
override fun onResponse(call: Call, response: Response) {
val inputStream = response.body?.string()
runOnUiThread{
mTextView.text = inputStream
}
}
})
}
}
produces this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vectortestapi, PID: 9900
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.vectortestapi/com.example.vectortestapi.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.example.vectortestapi.MainActivity> cannot be instantiated
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.vectortestapi.MainActivity> cannot be instantiated
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
I/Process: Sending signal. PID: 9900 SIG: 9
Abstract classes cannot be instantiated, but they can be subclassed.

How to use logger with Koin?

Koin version: 2.1.5
Instructions are missing from documentation here.
This is what I have:
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
// use AndroidLogger as Koin Logger - default Level.INFO
androidLogger()
// use the Android context given there
androidContext(this#MyApp)
// module list
modules(emptyList())
}
}
}
class MainActivity : AppCompatActivity() {
private val logger : Logger by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
logger.debug("onCreate")
}
}
And looks like there is no instance of Logger in the dependency graph:
04-26 12:02:03.781 4672-4672/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 4672
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: org.koin.core.error.NoBeanDefFoundException: No definition found for class:'org.koin.core.logger.Logger'. Check your definitions!
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: org.koin.core.error.NoBeanDefFoundException: No definition found for class:'org.koin.core.logger.Logger'. Check your definitions!
at org.koin.core.scope.Scope.throwDefinitionNotFound(Scope.kt:247)
at org.koin.core.scope.Scope.resolveInstance(Scope.kt:216)
at org.koin.core.scope.Scope.get(Scope.kt:181)
at com.example.myapplication.MainActivity$$special$$inlined$inject$1.invoke(ComponentCallbackExt.kt:51)
at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)
at com.example.myapplication.MainActivity.getLogger(MainActivity.kt)
at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:17)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
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:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Why?
My understanding: this logger is used for internal Koin logging. I came to the conclusion by looking into source code. This logger can be used outside of Koin itself getKoin()._logger.info("hello world") but I doubt it was the library creator's intention.

java.lang.NullPointerException: on a null object reference

package com.example.testapp
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.hanks.passcodeview.PasscodeView
class Main2Activity : AppCompatActivity() {
var EXTRA_TEXT ="fintech"
private var PRIVATE_MODE = 0
private val PREF_NAME = "myref"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val sharedPref: SharedPreferences = getSharedPreferences(PREF_NAME, PRIVATE_MODE)
var passcodeView=findViewById<PasscodeView>(R.id.passcodeView)
passcodeView.setPasscodeLength(4).listener = object :
PasscodeView.PasscodeViewListener{
override fun onSuccess(number: String?) {
if (sharedPref.getBoolean(PREF_NAME, false)) {
val homeIntent = Intent(applicationContext, AfterloginActivity::class.java)
homeIntent.putExtra("fintech",number)
startActivity(homeIntent)
finish()
} else {
setContentView(R.layout.activity_main2)
val editor = sharedPref.edit()
editor.putBoolean(PREF_NAME, true)
editor.apply()
}
}
override fun onFail() {
Toast.makeText(this#Main2Activity,"wrong",Toast.LENGTH_SHORT).show()
}
}
}
}
this my source code and i get this error:
Process: com.example.testapp, PID: 11939
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp/com.example.testapp.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.hanks.passcodeview.PasscodeView com.hanks.passcodeview.PasscodeView.setPasscodeLength(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.hanks.passcodeview.PasscodeView com.hanks.passcodeview.PasscodeView.setPasscodeLength(int)' on a null object reference
at com.example.testapp.Main2Activity.onCreate(Main2Activity.kt:46)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
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:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
04-02 14:39:57.454 11939-11939/? I/Process: Sending signal. PID: 11939 SIG: 9
if i put "setContentView(R.layout.activity_main2)" below oncreate its work but i want to make it onetime login only so i put it under and then i got this error "java.lang.NullPointerException: Attempt to invoke virtual method 'com.hanks.passcodeview.PasscodeView com.hanks.passcodeview.PasscodeView.setPasscodeLength(int)' on a null object reference"
Thanks
Your error is caused by this line:
var passcodeView=findViewById<PasscodeView>(R.id.passcodeView)
You are trying to find a view when you have not setContentView for the activity.
If you don't call setContentView() all calls to findViewById() will return null. passcodeView being null is the reason for your NullPointerException.
There is no way to start the AfterloginActivity before setContentView() because you rely on passCodeView to add something to the Intent when you do homeIntent.putExtra("fintech",number).

login to firebase in kotlin android studio

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.
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
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 :)

Categories

Resources