简体   繁体   中英

retrofit - kotlin - Parameter specified as non-null is null

I'm using mvvm , kotlin , retrofit and courtin in my app . 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"

this is my 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 :

@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

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

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注释?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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