probelalkhan / android-login-signup-tutorial

105 stars 57 forks source link

I just update the Api URL and I'm getting null reference error #3

Open alcarazolabs opened 3 years ago

alcarazolabs commented 3 years ago

Hello bro! I just update the URL of the datasource:

class RemoteDataSource {
    companion object {
        private const val BASE_URL = "http://192.168.0.4:5000/api/user/"
    }

This my AuthApi:


 @POST("login")
    suspend fun login(
        @Field("email") email: String,
        @Field("password") password: String
    ) : LoginResponse

Therefore to test this endpoit it should be: http://192.168.0.4:5000/api/user/login ok this works using Posman and this is my output:

{
    "user": {
        "id": "60760126e34a1d3668a264ac",
        "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2MDc2MDEyNmUzNGExZDM2NjhhMjY0YWMiLCJpYXQiOjE2MTgzNTQ1MzV9.VPRoCntI6uC4bOmaB9A9mVvzY8CSUxxS7VVB1W91KUI",
        "name": "Freddy Aal",
        "phone": "942912222",
        "email": "creatorxx@gmail.com",
        "created_at": "2021-04-13T20:37:58.123Z"
    }
}

I updated the class User according to my Output:

data class User(
    val id: String,
    val access_token: String?,
    val name: String,
    val phone: String,
    val email: String,
    val created_at: String
)

But when I run the project and test it.. always with a correct credentials or wrong credentials it always throw this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String net.simplifiedcoding.data.responses.User.getAccess_token()' on a null object reference
        at net.simplifiedcoding.ui.auth.LoginFragment$onActivityCreated$1$1.invokeSuspend(LoginFragment.kt:34)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:330)
        at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:26)
        at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:109)
        at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:158)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
        at kotlinx.coroutines.BuildersKt.launch(Unknown Source)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:49)
        at kotlinx.coroutines.BuildersKt.launch$default(Unknown Source)
        at net.simplifiedcoding.ui.auth.LoginFragment$onActivityCreated$1.onChanged(LoginFragment.kt:33)
        at net.simplifiedcoding.ui.auth.LoginFragment$onActivityCreated$1.onChanged(LoginFragment.kt:21)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at net.simplifiedcoding.ui.auth.AuthViewModel$login$1.invokeSuspend(AuthViewModel.kt:24)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6253)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

There is a but because of it: User.getAccess_token()' on a null object reference this code is in (LoginFragment.kt:34) Here is a picture: https://imgur.com/a/kIPtBnQ Please bro if you have any idea about this issue I will apreciate it to fix this problem

Thanks for the videos.

fail

alcarazolabs commented 3 years ago

Hi @probelalkhan bro I tested the app and works the login with my own Rest Api, the app then redirect to the dashboard but keep loading the loading picture.. and there is not info.. But the another bug is that if we login with a wrong user or wrong credentials the app got crash! because responses.User.getAccess_token()' on a null object reference.. with a null response the app got crash! My Api only is sending a json response {message: "wrong credentials" } Test the App again bro you have a bug... Please update it.. Thanks so much.

alcarazolabs commented 3 years ago

@probelalkhan Hi bro! I updated my Api Rest. Before I was sendig a server message: if(!user) return res.send({message: "wrong email"}) now I updated it responding wiht a 401 status code

if(!user)  return res.status(401).send('wrong email')

I did it using node.js and express.. Ok bro I update and the App works but how we can get the server message and show it instead of do it: requireView().snackbar("You've entered incorrect email or password") ? I want to show the server message instead of the snackbar message.. ¿Can you update it? thanks so much.