I'm using this code to parse a json file and change some items in de app I build:
fun fetchJson() {
if (isNetworkAvailable()) {
val request = Request.Builder().url(stationAPPJsonURL).build()
client.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
val body = response.body?.string()
val gson = GsonBuilder().create()
try {
val stationAPP = gson.fromJson(body, StationAPP::class.java)
//ACTIE
actie_zichtbaar = stationAPP.actie_zichtbaar
actie_img = stationAPP.actie_img
actie_url = stationAPP.actie_url
... I left out some code
catch (error: JsonParseException) {
runOnUiThread {
val toast = Toast.makeText(applicationContext, "Problem loading JSON.\nFunctionality may be limited for a while.", Toast.LENGTH_SHORT)
val view = toast.view!!.findViewById<TextView>(android.R.id.message)
toast?.let { view.gravity = Gravity.CENTER }
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
}
}
}
override fun onFailure(call: Call, e: IOException) {
runOnUiThread {
val toast = Toast.makeText(applicationContext, "Problem loading JSON.\nFunctionality may be limited for a while.", Toast.LENGTH_SHORT)
val view = toast.view!!.findViewById<TextView>(android.R.id.message)
view?.let { view.gravity = Gravity.CENTER }
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show() --> line 1390!!
}
}
}
}
I see in the logs in Google Play Console that sometime this part of the code crashes (4%). I've no clue why? I tested it on several devices and no errors at all.
Who can help me out here?
Is it possible That: val stationAPP = gson.fromJson(body, StationAPP::class.java) throws no exception but that somehow the json file is loaded with some errors in it? And therefor some items are null?
Thanks for you help ;)
Update: this is the error it produces:
Exception java.lang.NullPointerException:
at com.familiekoning.grolloo.MainActivity$fetchJson$1.onFailure$lambda-10 (MainActivity.kt:1390)
at com.familiekoning.grolloo.MainActivity$fetchJson$1.$r8$lambda$lczurdfMtnZdFDn9iwHvPqw9_Pg
at com.familiekoning.grolloo.MainActivity$fetchJson$1$$ExternalSyntheticLambda8.run
at android.os.Handler.handleCallback (Handler.java:938)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loop (Looper.java:250)
at android.app.ActivityThread.main (ActivityThread.java:7806)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:958)
CodePudding user response:
This is going to be a guess but if you see !!
anywhere and you have NullPointerException
. You are very close to certain that it is the culprit. If you want a different Toast appearance there are NPE safe ways.
Relying on stuff that is android.id.x
is not safe because it might be very much OEM dependent.
CodePudding user response:
I changed:
val toast = Toast.makeText(applicationContext, "Problem loading JSON.\nFunctionality may be limited for a while.", Toast.LENGTH_SHORT)
val view = toast.view!!.findViewById<TextView>(android.R.id.message)
view?.let { view.gravity = Gravity.CENTER }
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
to:
val toast = Toast.makeText(applicationContext, "Problem loading JSON.\nFunctionality may be limited for a while.", Toast.LENGTH_SHORT)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
That fixed the problem.