0

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)
11
  • What line/code throws the NPE? Commented Nov 3, 2022 at 13:31
  • Don't know ... I can't reproduce the error myself ... I think it could be because of a bad network. But just a guess. This is the error in Google Play Console: Commented Nov 3, 2022 at 14:00
  • See the error in the updated question. Thanks for your response ;) Commented Nov 3, 2022 at 14:02
  • What is on the line MainActivity.kt:1390? Commented Nov 3, 2022 at 14:02
  • Didn't know the Kotlin lines can be read from those errors ... thanks for that insight. Commented Nov 3, 2022 at 14:05

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thx, I set the gravity on the view (of the toast) which I try to fetch from android.id.x indeed. I now set it directly on toast and it works ;)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.