简体   繁体   English

为什么即使我反对,II也会得到零点异常?

[英]Why do I I get null point exception even though I safeguarded against it?

How do I safeguard against null points? 我如何防范零分? I tried this way: 我这样尝试:

var spr = context.getSharedPreferences("FAV", Context.MODE_PRIVATE)
var emptyArray: JsonArray = jsonArray()
var myjson = spr.getString("KEY", null)

//GET JSON FROM SHARED PREFERENCE

Log.d("TAG", "NOT NULL")
if(myjson != null) {
    var parsedFavFromJson = Gson().fromJson<List<String>>(myjson)
    //PARSE FROM JSON TO ARRAY

    for (i in parsedFavFromJson) {
        emptyArray.add(i)
        Log.d("TAG", "" + i)

        if (i == recipeArray!![counterX].recipeKey!!) {
            Picasso.with(context)
                    .load(R.drawable.fav_icon_60at3x).into(favMarkerButton)
            Log.d("TAG", "Match " + i)
        } else {
            Log.d("TAG", "WAS NULL")
            Picasso.with(context)
                    .load(R.drawable.fav_unclicked_icon60at3x).into(favMarkerButton)
        }
    }
}

I have clearly stated the conditional if(myjson != null) , still it pass through and crash with the error message java.lang.IllegalStateException: fromJson(json, typeToken<T>()) must not be null . 我已经明确说明了条件if(myjson != null) ,它仍然通过并崩溃并出现错误消息java.lang.IllegalStateException: fromJson(json, typeToken<T>()) must not be null There is no clear indication where the error actually occur - it actually points to an empty code line! 没有明确指示错误实际上在哪里发生的-它实际上指向空的代码行! If I remove the code above, things are working fine. 如果删除上面的代码,一切正常。 I also tried using: 我也尝试使用:

var myjson = spr.getString("KEY", null).let {

...and so on, with the same result. ...等等,结果相同。 Am I missing something here? 我在这里想念什么吗?

UPDATE I checked variable myjson and it is clearly null . UPDATE我检查变量myjson ,它显然 null Question is why the conditional if(myjson != null) passed... 问题是为什么条件if(myjson != null)通过...

In short, you're missing the question mark. 简而言之,您错过了问号。

You expect that in block: 您期望在块中:

spr.getString("KEY", null).let { it }

Won't be null? 不会为空吗? That's incorrect. 不对

Correct syntax is 正确的语法是

spr.getString("KEY", null)?.let { it }

Easy to miss. 容易错过。

Same goes for working with nested JSONs. 使用嵌套JSON也是如此。 You should never use bang-bang ( !! ) in Kotlin. 您永远不要在Kotlin中使用bang-bang( !! )。 Use the safe syntax instead: 请改用安全语法:

recipeArray?[counterX].recipeKey

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到空指针异常,即使println有值显示? - Why I am getting null pointer exception even though there is value show when println? 即使我发送了一个意图,但通过意图时,OnActivityResult也会为null - OnActivityResult get null as the intent passed even though I sent an intent 为什么我从TabWidget获得空指针异常? - Why do I get a null pointer exception from TabWidget? 为什么我在Android中收到Null Pointer异常 - Why I get Null Pointer Exception in Android 当我更改 Android 的屏幕方向时,为什么在 canvas 上出现 null 指针异常? - Why do I get a null pointer exception on canvas when I change the Android's screen orientation? 我对 Null 点异常有一些问题 - I have some problem with Null Point Exception 为什么即使我执行addToBackStack(null),FragmentManager的BackStackEntry仍为0 - Why does FragmentManager's BackStackEntry is 0 even though I perform addToBackStack(null) 即使我可以从ZipResourceFile对象读取内容,为什么我的“ content://” URI也返回空的InputStream? - Why does my “content://” URI return a null InputStream, even though I can read from the ZipResourceFile object? 即使我在 Android 中手动设置为 null,为什么 Flow (kotlinx.coroutines.flow) 无法使用 Retry? - Why does Flow (kotlinx.coroutines.flow) not working with Retry even though I manually set as null in Android? 为什么我总是会收到JSON Exception? - Why do I always get JSON Exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM