Ad

Retrofit Parameter Specified As Non-null Is Null

- 1 answer

I'm new in Kotlin and I'm trying to parse a simple JSON, but I'm getting an " Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter vouchers"

result.products is always null, but I can see in the logs that retrofit is getting correctly the json with a 200 ok request. So I suppose that could be a problem when I'm trying to parse the json

How can I solve this?

I have add my code below

disposable = ApiServe.getVouchers()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                { result -> processVouchers(result.products) },
                { error -> error(error.message)}
            )

fun processVouchers(vouchers : List<Product>){
        mCallback?.onResponseVouchers(vouchers)
    }

GET VOUCHERES in ApiServe class

@GET(Api.ENDPOINT.VOUCHER_ENDPOIN)
        fun getVoucher(): Observable<Response<Vouchers>>

MODEL

data class Voucher(val products: List<Product>)
data class Product(val code: String, val name: String, val price: Double)

JSON

{"products":[{"code":"Voucher","name":"Voucher","price":3},{"code":"Ball","name":"Voucher Ball","price":10},{"code":"Milk","name":"Voucher Milk","price":8.5}]}

Ad

Answer

I believe that the issue here might be that

 fun getVouchers(): Observable<Voucher.Vouchers>

Are you sure that getVoucher returns the correct type? Shouldn't it be Observable<Voucher> ?

Edit: It turned out that author was using excludeFieldsWithModifiers for his GsonConverterFactory, which was causing issues with parsing to model.

Ad
source: stackoverflow.com
Ad