简体   繁体   English

改造 - kotlin - 指定为非空的参数为空

[英]retrofit - kotlin - Parameter specified as non-null is null

I'm using mvvm , kotlin , retrofit and courtin in my app .我在我的应用程序中使用 mvvm 、 kotlin 、改造和 Courtin 。 I've done several request and all of them works fine but with this one , I get this error "Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter list"我已经完成了几个请求,并且所有请求都可以正常工作,但是在这个请求中,我收到此错误“指定为非空的参数为空:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数列表”

this is my json这是我的json

    {
"roomslist": [
{
"id": "1"
}
]
}

these are my models这些是我的模型

data class RoomsListModel(
    @Json(name = "roomslist")
    val roomsList: List<Rooms>
)

data class Rooms(

    @Json(name = "id")
    val id: String
}

this is my api interface :这是我的 api 接口:

@FormUrlEncoded
@POST("getPlaceRooms.php")
fun getPlaceRooms2(@Field("amlakid")id:String):Deferred<RoomsListModel>

this is my repository :这是我的存储库:

fun getRooms(
    amlakId: String
): MutableLiveData<RoomsListModel> {
    scope.launch {
        val request = api.getPlaceRooms2(amlakId)
        withContext(Dispatchers.Main) {
            try {
                val response = request.await()
                roomsLiveData.value = response
            } catch (e: HttpException) {
                Log.v("this", e.message);
            } catch (e: Throwable) {
                Log.v("this", e.message);
            }
        }
    }
    return roomsLiveData;
}

when the app runs , it goes into e: Throwable and returns the error当应用程序运行时,它进入 e: Throwable 并返回错误

my viewmodel我的视图模型

class PlacesDetailsViewModel : ViewModel() {
private val repository = PlaceDetailsRepository()


fun getRooms(amlakId: String):MutableLiveData<RoomsListModel> {
    return repository.getRooms(amlakId)
}
}

and this my activity request :这是我的活动请求:

viewModel.getRooms(amlakId).observe(this, Observer {
        vf.frm_loading.visibility = View.GONE

        it?.let {
            adapter.updateList(it?.roomsList)
            setNoItem(false)
        }

    })

I'm using moshi我正在使用 moshi

I've tried to clean ,rebuild but it doesn't make any different我试图清理,重建,但它没有任何不同

could you help me ?你可以帮帮我吗 ? what is going wrong with my code?我的代码出了什么问题?

You should try adding ?您应该尝试添加 ? to your Model parameters.到您的模型参数。 Not sure if in your case is the String?.不确定在您的情况下是否是字符串?。 It will ensure that you can have null values on your String它将确保您的字符串上可以有空值

val id: String?

Please double check, whatever value is missing or null in your case请仔细检查,在您的情况下,任何值丢失或为空

您是否尝试在val id: String声明中删除@Json注释?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM