IllegalArgumentException: Parameter specified as non-null is null

Solution 1:

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:

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:

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:

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?