IllegalArgumentException: Parameter specified as non-null is null

AndroidExceptionNullKotlin

Android Problem Overview


I'm getting the following runtime error:

 checkParameterIsNotNull, parameter oneClickTokens
    at com.info.app.fragments.Fragment_Payment_Profile$fetchMerchantHashes$1.onPostExecute(Fragment_Payment_Profile.kt:0)
    at com.info.app.fragments.Fragment_Payment_Profile$fetchMerchantHashes$1.onPostExecute(Fragment_Payment_Profile.kt:1543)

Here's my code:

 private fun fetchMerchantHashes(intent: Intent) {
    // now make the api call.
    val postParams = "merchant_key=$key&user_credentials=$var1"
    val baseActivityIntent = intent
    object : AsyncTask<Void, Void, HashMap<String, String>>() {

        override fun doInBackground(vararg params: Void): HashMap<String, String>? {
                ...
        }

        override fun onPostExecute(oneClickTokens: HashMap<String, String>) {
            super.onPostExecute(oneClickTokens)
            ...

        }
    }.execute()
}

It seems that the function call seems to be invalid. However, I don't know how to fix this problem. Is there anything Kotlin specific I've missed?

Android Solutions


Solution 1 - Android

The exception is pretty clear: you're passing null for the parameter.

By default all variables and parameters in Kotlin are non-null. If you want to pass null parameter to the method you should add ? to it's type, for example:

fun fetchMerchantHashes(intent: Intent?)

For more information: null-safety.

Solution 2 - Android

In my case this error warned about probable passing a null as a parameter. Three ways of correction.

  1. Change a type of a parameter in corresponding place. If it is received from Java, add @NonNull annotation to a variable definition.
  2. Add !! to an argument of the method.
  3. Add ? to the parameter in Kotlin class. I think, Kotlin conversion tool might do this by default, if in Java class there were no annotations used (like @Nullable, @NonNull).

Solution 3 - Android

Simple Answer Will Work For Sure... When you are fetching the data from the Server using Rest Client (Web Services calls) (GET Or POST) and if there could be a null parameter value in the json response, and you are fetching the parameter and appending it to textview you get this error..

Solution: just append ? mark to the variable as below..

Example:

var batchNo: String? = "" (Correct Approach)
var batchNo= "" (Will Get Error)

Here I am trying to fetch batchNo using service call.

Solution 4 - Android

I caught similar exception after converting an Activity from Java to Kotlin using Android Studio converting tool. So, it's that I got override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent)

It's right override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)

Difference in intent: Intent?

Solution 5 - Android

For each of the binding adapters, change the type of the item argument to nullable, and wrap the body with

> item?.let{...}

source in google documentation

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionA MaharajaView Question on Stackoverflow
Solution 1 - AndroidAlexander RomanovView Answer on Stackoverflow
Solution 2 - AndroidCoolMindView Answer on Stackoverflow
Solution 3 - AndroidAmbilpura Sunil KumarView Answer on Stackoverflow
Solution 4 - AndroidAskar SyzdykovView Answer on Stackoverflow
Solution 5 - AndroidyusufgltcView Answer on Stackoverflow