I do a video lesson. https://www.youtube.com/watch?v=Vcn4OuV4Ixg I ran into an error, I can not fix it in any way. Help.

Unresolved reference: data. (fetchData.kt)

MainActivity.kt

package com.example.sfp.parsejson import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Button import android.widget.TextView import kotlin.system.exitProcess class MainActivity : AppCompatActivity() { lateinit var click:Button lateinit var data:TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) click = findViewById(R.id.button) as Button data = findViewById(R.id.fetchedata) as TextView click.setOnClickListener(object:View.OnClickListener{ override fun onClick(view:View) { val process = fetchData() process.execute() } }) } } 

fetchData.kt

 package com.example.sfp.parsejson import android.os.AsyncTask import org.json.JSONArray import org.json.JSONException import org.json.JSONObject import java.io.BufferedReader import java.io.IOException import java.io.InputStreamReader import java.net.HttpURLConnection import java.net.MalformedURLException import java.net.URL class fetchData : AsyncTask<Void, Void, Void>() { var data: String = "" private var dataParsed = "" private var singleParsed = "" override fun doInBackground(vararg voids: Void): Void? { try { val url = URL("https://api.myjson.com/bins/b1kxl") val httpURLConnection = url.openConnection() as HttpURLConnection val inputStream = httpURLConnection.inputStream val bufferedReader = BufferedReader(InputStreamReader(inputStream)) var line: String? = "" while (line != null) { line = bufferedReader.readLine() data += line!! } val JA = JSONArray(data) for (i in 0 until JA.length()) { val JO = JA.get(i) as JSONObject singleParsed = "Name:" + JO.get("name") + "\n" + "Password:" + JO.get("password") + "\n" + "Contact:" + JO.get("contact") + "\n" + "Country:" + JO.get("country") + "\n" dataParsed += singleParsed } } catch (e: MalformedURLException) { e.printStackTrace() } catch (e: IOException) { e.printStackTrace() } catch (e: JSONException) { e.printStackTrace() } return null } override fun onPostExecute(aVoid: Void) { super.onPostExecute(aVoid) MainActivity.data.setText(this.dataParsed) } } 

activity_mail.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.sfp.parsejson.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:text="click me" android:id="@+id/button"/> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/button"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" android:textSize="24sp" android:hint="Fetched Text Here!" android:id="@+id/fetchedata"/> </ScrollView> </RelativeLayout> 

JSON https://api.myjson.com/bins/b1kxl

 [{"name":"Ivan","password":"try","contact":"russia@mail.ru","country":"Russia"},{"name":"Layo Choo","password":"122223qweer","contact":"china@mail.ch","country":"China"},{"name":"Johan","password":"333qweer","contact":"deutsch@mail.de","country":"Germany"}] 

And such an error when using the TextView type.

 09-11 14:43:26.136 3804-5815/com.example.sfp.parsejson E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.example.sfp.parsejson, PID: 3804 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:309) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) Caused by: kotlin.KotlinNullPointerException at com.example.sfp.parsejson.fetchData.doInBackground(fetchData.kt:32) at com.example.sfp.parsejson.fetchData.doInBackground(fetchData.kt:18) at android.os.AsyncTask$2.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) 

 package com.example.sfp.parsejson import android.os.AsyncTask import android.util.Log import android.widget.TextView import org.json.JSONArray import org.json.JSONException import org.json.JSONObject import java.io.BufferedReader import java.io.IOException import java.io.InputStreamReader import java.net.HttpURLConnection import java.net.MalformedURLException import java.net.URL class fetchData : AsyncTask<Void, Void, TextView>() { var data: String = "" private var dataParsed = "" private var singleParsed = "" override fun doInBackground(vararg p0: Void?): TextView? { try { val url = URL("https://api.myjson.com/bins/b1kxl") val httpURLConnection = url.openConnection() as HttpURLConnection val inputStream = httpURLConnection.inputStream val bufferedReader = BufferedReader(InputStreamReader(inputStream)) var line: String? = "" while (line != null) { line = bufferedReader.readLine() data += line!! } val JA = JSONArray(data) for (i in 0 until JA.length()) { val JO = JA.get(i) as JSONObject singleParsed = "Name:" + JO.get("name") + "\n" + "Password:" + JO.get("password") + "\n" + "Contact:" + JO.get("contact") + "\n" + "Country:" + JO.get("country") + "\n" dataParsed += singleParsed } } catch (e: MalformedURLException) { e.printStackTrace() } catch (e: IOException) { e.printStackTrace() } catch (e: JSONException) { e.printStackTrace() } return null } override fun onPostExecute(aVoid: TextView) { super.onPostExecute(aVoid) aVoid.text = this.dataParsed } } 
  • And what a mistake? Add a stack of tracks - gil9red
  • Error: (56, 22) Unresolved reference: data; Error: Execution failed for task ': app: compileDebugKotlin'. > Compilation error. See log for more details - nuqss
  • This is the text of the error, not the stack of the trace, the stack describes all the function calls up to the last one, in which the error occurred, you can understand by the stack exactly where the problem was. And so, presumably the problem in MainActivity.data.setText(this.dataParsed) is gil9red
  • How to create a stack in Android Studio? - nuqss
  • five
    You can say that I'm carping, but the title is just a fat minus. He should be speaking, and not a cry of a man who has softened with his brain - Aleksey Shimansky

1 answer 1

Try this

MainActivity.kt

 class MainActivity : AppCompatActivity() { lateinit var click:Button lateinit var data:TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) click = findViewById(R.id.button) as Button data = findViewById(R.id.fetchedata) as TextView click.setOnClickListener(object:View.OnClickListener{ override fun onClick(view:View) { fetchData(data).execute() } }) } } 

fetchData.kt

 class fetchData(val textView: TextView) : AsyncTask<Void, Void, String>() { var data: String = "" private var dataParsed = "" private var singleParsed = "" override fun doInBackground(vararg p0: Void?): String? { try { val url = URL("https://api.myjson.com/bins/b1kxl") val httpURLConnection = url.openConnection() as HttpURLConnection val inputStream = httpURLConnection.inputStream val bufferedReader = BufferedReader(InputStreamReader(inputStream)) for (line in bufferedReader.readLine()){ data+=line; } val JA = JSONArray(data) for (i in 0 until JA.length()) { val JO = JA.get(i) as JSONObject singleParsed = "Name:" + JO.get("name") + "\n" + "Password:" + JO.get("password") + "\n" + "Contact:" + JO.get("contact") + "\n" + "Country:" + JO.get("country") + "\n" dataParsed += singleParsed } } catch (e: MalformedURLException) { e.printStackTrace() } catch (e: IOException) { e.printStackTrace() } catch (e: JSONException) { e.printStackTrace() } return dataParsed } override fun onPostExecute(aVoid: String) { super.onPostExecute(aVoid) textView.text = aVoid } } 
  • An application crashes with an error. FATAL EXCEPTION: AsyncTask # 1; Process: com.example.sfp.parsejson, PID: 11593; java.lang.RuntimeException:
  • @userBuzzer, what you have written to us about nothing does not say, the most interesting thing in the glass frame usually begins with the words "Caused by", but rather a full stack of errors and learn to understand for yourself what is written there and where it points to the code. - woesss
  • @userBuzzer updated the answer - Rasul A-