Good afternoon, I can't get the data from the List in the getUser () method, because I get an error saying that the List has no data. Although they are there, the ParserKtJson () method displays data in the log.

class ParserKtJson { private var users: List<User>? = null fun createFromJson(mJson: String): ParserKtJson { val gson = GsonBuilder().create() val pj = ParserKtJson() pj.users = Arrays.asList(*gson.fromJson(mJson, Array<User>::class.java)) Log.d("**** **** ***", pj.users!!.get(1).id.toString()) // 2 getUser() return pj } fun getUser(): List<User>? { val pj = ParserKtJson() Log.d("**** ****", pj.users!!.get(1).id.toString()) // kotlin.KotlinNullPointerException return users; } } 

Log

 Process: ua.com.developmentunderandroid.developmentunderandroid, PID: 8886 kotlin.KotlinNullPointerException at ua.com.developmentunderandroid.developmentunderandroid.web.kt.ParserKtJson.getUser(ParserKtJson.kt:28) at ua.com.developmentunderandroid.developmentunderandroid.web.kt.ParserKtJson.ParserKtJson(ParserKtJson.kt:21) at ua.com.developmentunderandroid.developmentunderandroid.KtWeb.parsingKt(KtWeb.kt:97) at ua.com.developmentunderandroid.developmentunderandroid.KtWeb$ProgressTask.onPostExecute(KtWeb.kt:57) at ua.com.developmentunderandroid.developmentunderandroid.KtWeb$ProgressTask.onPostExecute(KtWeb.kt:41) at android.os.AsyncTask.finish(AsyncTask.java:636) at android.os.AsyncTask.access$500(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5293) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

    1 answer 1

    Here

     fun getUser(): List<User>? { val pj = ParserKtJson() Log.d("**** ****", pj.users!!.get(1).id.toString()) // kotlin.KotlinNullPointerException return users; } 

    you put an instance of the class ParserKtJson in the variable pj. In it, the users field is explicitly initialized as null . After you turn to him and are expected to fall.

    Apparently, you meant that you will call a constructor, which is similar to the fun ParserKtJson method fun ParserKtJson - but this is not a constructor and you do not call this method anywhere.

    • Sorry, I made a mistake in the method name. Actually I did not describe the designer. I tried and without a copy of Log.d("**** ****", users!!.get(1).id.toString()) did not help. Java also had such a problem, I solved it by specifying the List <User> static - Sergiyss
    • I managed to get the data from the List in this way var kt = ParserKtJson(); contentView!!.text = kt.createFromJson(content).getUser()!!.get(0).id.toString() var kt = ParserKtJson(); contentView!!.text = kt.createFromJson(content).getUser()!!.get(0).id.toString() - Sergiyss
    • It turns out that so quickly the data is deleted from the List? - Sergiyss
    • one
      @Sergiyss You create a new object of the class ParserKtJson in each method, therefore users will always be null, you need to return this - Bleser