简体   繁体   English

尝试在 null object 参考上调用虚拟方法...

[英]Attempt to Invoke virtual method ... on a null object reference

I get a null object reference Exception message everytime I try filter data from Firebase Database and add them into my RecyclerView .每次我尝试从 Firebase 数据库过滤数据并将它们添加到我的RecyclerView时,我都会收到 null object 参考异常消息。

If I pass when(id) { "" -> {...}} everything is fine all Items which I want will be added to the RecyclerView , but if I pass when(id) { else -> {...}} it shows me a null object reference Exception.如果我通过when(id) { "" -> {...}}一切都很好,我想要的所有项目都将添加到RecyclerView ,但是如果我通过when(id) { else -> {...}}它向我展示了when(id) { else -> {...}} object 参考异常。

- Exception is below the code - - 异常在代码下方 -

fun fetchVideos(id: String?) {
        if (id == currentFilter) {
            currentFilter = null
            return fetchSubs()
        }

        currentFilter = id
        val uid = auth.uid
        val ref = database.getReference("/content/videos")
        ref.addListenerForSingleValueEvent(object: ValueEventListener {

            @SuppressLint("NotifyDataSetChanged")
            override fun onDataChange(p0: DataSnapshot) {
                val adapter = GroupAdapter<GroupieViewHolder>()
                p0.children.forEach {
                    val video = it.getValue(Video::class.java)

                    when(id) {
                        "" -> {
                            if (video != null && video.publisher_uid != uid) {
                                val user = database.getReference("users/$uid/subscriptions/${video.publisher_uid}")
                                user.addListenerForSingleValueEvent(object : ValueEventListener{
                                    override fun onDataChange(p0: DataSnapshot) {
                                        val sub = p0.getValue(Subscription::class.java) ?: return
                                        if (sub.subscribed == true){
                                            adapter.add(VideoItem(video))
                                        }
                                    }
                                    override fun onCancelled(error: DatabaseError) {

                                    }
                                })
                            }
                        }
                        else -> {
                            if (video != null && video.publisher_uid == id) {
                                adapter.add(VideoItem(video))
                            }
                        }
                    }
                }

                // New Bottom Sheet
                adapter.setOnItemClickListener{ item, view ->
                    val videoItem = item as VideoItem
                    selectedVideo = videoItem.video.video_uid
                    HomeFragment.selectedVideo = selectedVideo
                    updateViewCount()
                    addRecentVideo()
                    showVideoPlayer()
                    createValues()
                }

                try {
                    sub_recyclerview_videos.adapter = adapter
                }catch (e: Exception){
                    Log.d(TAG,"${e.message}")
                }
                adapter.notifyDataSetChanged()
            }
            override fun onCancelled(p0: DatabaseError) {

            }
        })
    }

This is my Exception:这是我的例外:

Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference尝试在 null object 参考上调用虚拟方法 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)'

The Problem was not the adapter or my layout it was actually my class SubItem .问题不是适配器或我的布局,它实际上是我的 class SubItem

class SubItem(val channel: Channel) : Item<GroupieViewHolder>() {
    @SuppressLint("ResourceAsColor")
    override fun bind(viewHolder: GroupieViewHolder, position: Int) {
        var selected_position = SubscriptionsFragment.selected_position
        val profileImageUrl =  channel.channel_logo

        viewHolder.itemView.sub_item_name.text = channel.channel_name

        viewHolder.itemView.sub_item_layout.setBackgroundResource(R.color.white)
        viewHolder.itemView.sub_item_name.setTextColor(R.color.colorSecondaryText)

        val targetImageView = viewHolder.itemView.sub_item_profile
        try {
            Picasso.get().load(profileImageUrl)
                .placeholder(R.drawable.ic_baseline_account_circle_24)
                .into(targetImageView)
        }catch (e:Exception){
            Log.d("SubItem","${e.message}")
//            Toast.makeText(,e.message,Toast.LENGTH_SHORT).show()
        }

        /*viewHolder.itemView.sub_item_layout.setOnClickListener {
            selected_position = position

            // because I am calling here it doesn't know my recyclerview
            SubscriptionsFragment().fetchVideos(channel.uid) // because I am calling here fetchVideos it blocked another clickListener for the same object
            notifyChanged()
        }*/

        checkFilter(viewHolder,position)
        if (SubscriptionsFragment.list[position]){
            viewHolder.itemView.sub_item_layout.setBackgroundResource(R.color.colorSecondaryText)
            viewHolder.itemView.sub_item_name.setTextColor(R.color.black)
        }
        else{
            viewHolder.itemView.sub_item_layout.setBackgroundResource(R.color.white)
            viewHolder.itemView.sub_item_name.setTextColor(R.color.colorSecondaryText)
        }
    }

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

相关问题 尝试在空对象引用上调用虚拟方法 - Attempt to invoke virtual method on a null object reference 尝试在空对象参考上调用虚拟方法“…” - Attempt to invoke virtual method '…' on null object Reference 尝试调用虚方法 - 空对象引用 - Attempt to invoke virtual method - Null object reference 尝试在空对象引用上调用虚拟方法“…………” - Attempt to invoke virtual method '.........' on a null object reference 尝试对空对象引用调用虚拟方法“” - Attempt to invoke virtual method ' 'on a null object reference 尝试在空对象引用上调用虚拟方法 - Attempt to invoke virtual method on a null object reference 尝试在空对象引用上调用虚拟方法,但对象不为空 - Attempt to invoke virtual method on a null object reference but the object is not null 尝试在null对象引用上调用虚拟方法android.widget.EditText.getText() - Attempt to invoke virtual method on Attempt to invoke virtual method android.widget.EditText.getText() on a null object reference Android:尝试对空对象引用调用虚拟方法 - Android: Attempt to invoke virtual method on a null object reference 尝试在空对象引用上调用虚拟方法“Context.getSharedPreferences()” - Attempt to invoke virtual method 'Context.getSharedPreferences()' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM