kotlin.UninitializedPropertyAccessException:lateinit属性salesGST模型类尚未安装

时间:2019-04-10 09:25:59

标签: kotlin retrofit2 rx-java2

使用Retrofit2和rxjav2进行Json数据解析。那么它给salesGST的lateinit属性错误:

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

在ArrayList中的SalesGST数据之后,JSONObject键中的模型类SalesGSTList成功。

class SalesGSTList {

    val success : String = ""

    lateinit var salesGST: ArrayList<SalesGST>
}

在SalesGST中声明JSON键。

class SalesGST {

    var FYearID : Int = 0

    var Cmp_Name : String? = ""

    var GSTIN : String? = ""

    var FirmName : String? = ""
}

解析Json数据在Textview和TableLayout中显示。数据订阅时出现错误。

sales.kt

class Sales : AppCompatActivity() {

    internal lateinit var api : APIInterface

    private var compositeDisposable : CompositeDisposable? = null

    private var salesGST : SalesGST? = 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(cid,fid,fDate,tDate)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { displaySalesGSTData(it.salesGST) })*/

        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()


        }))



    }


    private fun displaySalesGSTData(salesGSt : List<SalesGST>) {

        salesGST = SalesGST()
        tvSalesCompanyName.setText(salesGST!!.Cmp_Name)
        tvGSTIN.setText(salesGST!!.GSTIN)


        val rowHeader = TableRow(this@Sales)
        rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0"))
        rowHeader.setLayoutParams(TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT))
        val headerText = arrayOf<String>("Sr.No.", "Invoice Type", "Bill No.", "Bill Date", "Firm Name", "GST NO","TAX Total","CGST","SGST","IGST","Net Amount")
        for (c in headerText)
        {
            val tv = TextView(this@Sales)
            tv.setLayoutParams(TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                    TableRow.LayoutParams.WRAP_CONTENT))
            tv.setGravity(Gravity.CENTER)
           // tv.setBackgroundResource(R.drawable.table_header)
            tv.setTextColor(Color.parseColor("#3F51B5"))
            tv.setTextSize(18F)
            tv.setPadding(5, 5, 5, 5)
            tv.setText(c)
            rowHeader.addView(tv)
        }
        tableMarks.addView(rowHeader)

        for (j in 0 until salesGSt.size)
        {

            var fName : String = salesGSt[j].FirmName!!
            var invoice : String = salesGSt[j].InvoiceType!!
            var bill_no : String = salesGSt[j].ChallanNo!!
            var bill_date : String = salesGSt[j].ChallanDate!!
            var gst_no : String = salesGSt[j].PartyGST!!
            var tax_total : String = salesGSt[j].TaxTotal!!
            var cgst : String = salesGSt[j].CGSTTotal!!
            var igst : String = salesGSt[j].IGSTTotal!!
            var sgst : String = salesGSt[j].SGSTTotal!!
            var net_amount : String = salesGSt[j].ChallanAmount!!

            var sr : Int = j
            // dara rows
            val row = TableRow(this@Sales)
            row.setLayoutParams(TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT))
            val colText = arrayOf<String>(sr.toString(),(invoice), bill_no, bill_date, fName, gst_no,tax_total,cgst,sgst,igst,net_amount)
            for (text in colText)
            {
                val tv = TextView(this@Sales)
                tv.setLayoutParams(TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
                        TableRow.LayoutParams.WRAP_CONTENT))
                tv.setGravity(Gravity.CENTER)
               // tv.setBackgroundResource(R.drawable.table_shape)
                tv.setTextSize(18F)
                tv.setTextColor(Color.parseColor("#000000"))
                tv.setPadding(5, 5, 5, 5)
                tv.setText(text)
                row.addView(tv)
            }
            tableMarks.addView(row)
        }
    }
}

接口

@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 :(得分:0)

这个错误很明显。您尝试在初始化之前使用变量salesGST。

我想这是从这里来的

class SalesGSTList {

    val success : String = ""
    // salesGST expects to be initialize soon and is never initialized
    lateinit var salesGST: ArrayList<SalesGST>
}

您应该对salesGST进行延迟的初始化,或者将其设置为:

var salesGST: ArrayList<SalesGST>? = null