首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Поменяласьструктураответа。W/System.err: java.lang.NullPointerException

Поменяласьструктураответа。W/System.err: java.lang.NullPointerException
EN

Stack Overflow用户
提问于 2021-05-14 10:42:19
回答 1查看 56关注 0票数 0

应用程序正在工作,但是在响应的结构和授权期间,(登录+通用令牌)不包括在应用程序中。

代码:

代码语言:javascript
复制
suspend fun authorize(phone: String, password: String) : AccessTokenResponse? {

        //safeApiCall is defined in BaseRepository.kt (https://gist.github.com/navi25/67176730f5595b3f1fb5095062a92f15)
        val response = safeApiCall(
            call = {api.authorize(AuthorizeRequest(phone = phone, password = password)).await()},
            errorMessage = "Error Fetching Popular Movies"
        )

        return accessToken(response?.data?.items?.getOrNull(0)?.authorizationCode!!)
    }

    suspend fun accessToken(authorizationCode: String) : AccessTokenResponse? {

        //safeApiCall is defined in BaseRepository.kt (https://gist.github.com/navi25/67176730f5595b3f1fb5095062a92f15)
        val response = safeApiCall(
            call = {api.accessToken(AccessTokenRequest(authorizationCode = authorizationCode)).await()},
            errorMessage = "Error Fetching Popular Movies"
        )

        return response?.data?.items?.getOrNull(0)
    }

--即,它给出了行上的一个错误:

代码语言:javascript
复制
return accessToken(response?.data?.items?.getOrNull(0)?.authorizationCode!!)

结构AccessTokenResponse:

代码语言:javascript
复制
data class AccessTokenResponse(
    @SerializedName("access_token")
    val accessToken: String,

    @SerializedName("expires_at")
    val expiresAt: Long,

    @SerializedName("refresh_token")
    val refreshToken: String,

    @SerializedName("refresh_expires_at")
    val refreshExpiresAt: Long
)

结构AuthorizeResponse:

代码语言:javascript
复制
data class AuthorizeResponse(
    @SerializedName("authorization_code")
    val authorizationCode: String,

    @SerializedName("expires_at")
    val expiresAt: Long
)

控制台:

代码语言:javascript
复制
W/System.err: java.lang.NullPointerException
        at com.example.ferry.data.api.FerryRepository.authorize(FerryRepository.kt:29)
        at com.example.ferry.data.api.FerryRepository$authorize$1.invokeSuspend(Unknown Source:12)
        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:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8512)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

json:

代码语言:javascript
复制
{
        "result": true,
        "errors": null,
        "items": [
            {
                "user": [
                    {
                       ...
                    }
                ],
                "authorization_code": "1234352353245dsfasfasfdasf",
                "expires_at": 1620982345
            }
        ],
        "versions": {
           ...
        }
    }

我非常感谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2021-05-18 21:47:58

您必须在那里处理空异常。例如,您可以使用这样的方法。我还没试过这个密码。我希望你明白重点。

在那里,当您想要使用这个suspend fun时,您需要再次检查这个fun是否为null。

代码语言:javascript
复制
if (response != null) {
     return accessToken(response?.data?.items?.getOrNull(0)?.authorizationCode!!)
}
return null
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67533000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档