簡體   English   中英

Kotlin Null 指針異常

[英]Kotlin Null pointer exception

我正在嘗試顯示存儲在 firebase 數據庫中的用戶的電子郵件。 我有這個下面的代碼

    snapsListView = findViewById(R.id.snapsList)
    val adapter = emails?.let { ArrayAdapter(this, android.R.layout.simple_list_item_1, it) }
    snapsListView?.adapter = adapter

    auth.currentUser?.uid?.let { FirebaseDatabase.getInstance().getReference().child("users").child(it).child("snaps").addChildEventListener(object: ChildEventListener{
        override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
            emails?.add(snapshot.child("from").value as String)

            adapter?.notifyDataSetChanged()
            try {
                for(x in emails!!){
                    Log.i("Emails", x)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

電子郵件存在於數據庫中,並且根據層次結構,users-> uid->snaps 和 snaps 在“from”中包含 email

不知道是什么導致了這個錯誤,或者我該如何解決這個問題。

總是得到 null 指針異常

通過使用!! 運營商您已斷言emails不是null 但是,Kotlin 引發了運行時異常,因為emails實際上是null 考慮更換!! 安全訪問:

emails?.let { for (x in it) Log.i("Emails", x) }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM