today I have tried running my app, but and have a problem, that i don't know it is. Yesterday worked correctly.
If I comment the indicated line the application runs perfectly
Thanks for your help.
This is the error:
FATAL EXCEPTION: main
Process: com.example.tribuum_app_2022, PID: 7542
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tribuum_app_2022/com.example.tribuum_app_2022.ui.view.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.- wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException
at com.example.tribuum_app_2022.ui.view.MainActivity.validateUserLogin(MainActivity.kt:37)
at com.example.tribuum_app_2022.ui.view.MainActivity.onCreate(MainActivity.kt:30)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.- wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
The line where the mistake exist
private fun validateUserLogin(){
val guardarEnXml = getSharedPreferences("datos_usuario_login", MODE_PRIVATE)
txtUserLoginIdShare = guardarEnXml.getString("id", null)
txtUserLoginPassShare = guardarEnXml.getString("pass", null)
Line 37: userLogin = UserLogin(txtUserLoginIdShare!!,txtUserLoginPassShare!!)
userViewModel.loginUser(userLogin)
}
The class of the object:
package com.example.app_2022.domain.model
import com.example.app_2022.data.model.UserLoginModel
data class UserLogin(var id: String?, var pass: String?)
fun UserLoginModel.toDomain() = UserLogin(id,pass)
The other class that change the type of the object:
package com.example.app_2022.data.model
import com.example.app_2022.domain.model.UserLogin
import com.google.gson.annotations.SerializedName
data class UserLoginModel(
@SerializedName("idUsuario") var id: String?,
@SerializedName("contrasena") val pass: String?
)
fun UserLogin.toDatabase() = UserLoginModel(id = id, pass = pass)
The logcat is saying there is a
NullPointerExceptionin line 37. So theretxtUserLoginIdShareandtxtUserLoginPassSharecan be null. Because all the values fromSharedPreferencescan be null. So you should check it before use, instead of forcing it toNotNullby using!!.