kotlin.UninitializedPropertyAccessException:lateinit属性salesGST尚未初始化

时间:2019-04-09 08:39:33

标签: kotlin retrofit2

当参数传递给接口然后给出错误时,Json Server数据在Android中使用retrofit2和rxjava2显示。

class Sales : AppCompatActivity() {

    internal lateinit var api : APIInterface

    private var compositeDisposable : CompositeDisposable? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sales)


        compositeDisposable = CompositeDisposable()

       fetchYearData()
    }

  private fun fetchYearData(){

        val retrofit = APIClient.apIClient
        if (retrofit != null) {
            api = retrofit.create(APIInterface::class.java)
        }


   compositeDisposable!!.add(api.getSalesGSTData(1,1,"04/01/2018","31/03/2019")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( { displaySalesGSTData(it.salesGST) },
                {

                    Toast.makeText(this,it.message,Toast.LENGTH_LONG).show()


        }))



    }

模型类SalesGSTList JsonObject成功之后,在JSONArray和JSONObject之后。

SalesGSTList.kt

class SalesGSTList {

    val success : String = ""

    lateinit var salesGST : ArrayList<SalesGST>
}

SalesGST.kt

class SalesGST {

    var FYearID : Int = 0

    var Cmp_Name : String? = ""

    var GSTIN : String? = ""
}

接口

@GET("SalesGST.php")
    fun getSalesGSTData(@Query("cid") cid : Int,@Query("fid") fid : Int,@Query("fd") fd : String,@Query("td") td : String) : Observable<SalesGSTList>

1 个答案:

答案 0 :(得分:1)

只需从<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>的{​​{1}}行中删除lateinit

lateinit var salesGST : ArrayList<SalesGST>

有关更多信息,请访问kotlin-when-to-use-lazy-or-lateinit