IllegalArgumentException: Parameter specified as non-null is null

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

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?

回答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.



回答2:

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

  1. Change a type of parameter in corresponding place. If it is received from Java, add @NonNull annotation to a variable definition.
  2. Add !! to a parameter of the method.


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!