Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalArgumentException: Parameter specified as non-null is null

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?

like image 368
A Maharaja Avatar asked Jul 03 '17 12:07

A Maharaja


4 Answers

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.

like image 160
Alexander Romanov Avatar answered Nov 18 '22 05:11

Alexander Romanov


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).
like image 23
CoolMind Avatar answered Nov 18 '22 06:11

CoolMind


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.

like image 3
Ambilpura Sunil Kumar Avatar answered Nov 18 '22 05:11

Ambilpura Sunil Kumar


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?

like image 1
Askar Syzdykov Avatar answered Nov 18 '22 05:11

Askar Syzdykov