0

I have a question about this error. So I just launch my application on google play and I got a lot of crash reports with detail like this

java.lang.NullPointerException: 
at c.c.f.b.onPostExecute (:1)
at android.os.AsyncTask.finish (AsyncTask.java:771)
at android.os.AsyncTask.access$900 (AsyncTask.java:199)
at android.os.AsyncTask$InternalHandler.handleMessage (AsyncTask.java:788)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loop (Looper.java:257)
at android.app.ActivityThread.main (ActivityThread.java:8218)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:626)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1015)

But when I test it on my device and emulator, I never got that crash or error. There are some crash errors like that, but with different details.

I'm using Android Studio with Kotlin. I would really appreciate it if anyone can help me to solve this error.

Thank you so much.

==============================

I have a single class called DataApi.kt and it is called once from MainActivity.class. Inside the DataApi.kt, I override the onPostExecute method to save some data to SharedPreference that I need.

DataAPI.kt

class DataAPI : AsyncTask<String?, String?, String>() {
    val JSON = MediaType.parse("application/json; charset=utf-8")
    private lateinit var mContext: Context

    fun DataAPI(context: Context) {
        mContext = context
    }

    override fun doInBackground(vararg p0: String?): String? {
        /* some code */
    }

    override fun onPostExecute(result: String) {
        super.onPostExecute(result)
        try {
            val jObj = JSONObject(result)
            PreferenceConnector.clearShared(mContext)
            PreferenceConnector.writeString(
                mContext,
                PreferenceConnector.DOMAIN,
                jObj.getString("domain")
            )
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )

        var api = DataAPI()
        api.DataAPI(this)
        api.execute()
        Handler().postDelayed({ init() }, 1000)
    }
foxie fox
  • 35
  • 6
  • About the `onPostExecute` you can check this code https://stackoverflow.com/a/60289320/11835023 – shrimpcolo Sep 30 '21 at 04:27
  • 1
    Your `doInBackground` can return nulls (`String?`) but `onPostExecute` expects nonnulls (`String`). – laalto Sep 30 '21 at 07:58
  • 1
    I agree with @laalto's comment, from the crash log `c.c.f.b.onPostExecute (:1)` and the return value of `doInBackground` maybe is null, but `onPostExecute` only accept non-null String. Moreover, the param of `JSONObject(result)` expect non-null String, `JSONObject(@RecentlyNonNull String json)` – shrimpcolo Sep 30 '21 at 10:36

1 Answers1

0

Regards the NPE(NullPointerException), the crash report shows the crash in the method of onPostExecute

Maybe you use sth. View in onPostExecute, but you didn't initialize it.

shrimpcolo
  • 249
  • 2
  • 8
  • Hi, thank you for your reply, I update my question to add more detail about the flow that I'm implementing on my project. I didn't update View onPostExecute, I just save some data on SharedPreference – foxie fox Sep 30 '21 at 07:06