尝试调用虚拟方法...在 null 对象引用上

Attempt to Invoke virtual method ... on a null object reference

提问人:Andrey 提问时间:9/14/2022 更新时间:9/15/2022 访问量:958

问:

每次尝试从 Firebase Database 过滤数据并将它们添加到我的 .RecyclerView

如果我传递一切正常,我想要的所有项目都将被添加到 中,但如果我传递它向我显示一个空对象引用异常。when(id) { "" -> {...}}RecyclerViewwhen(id) { else -> {...}}

- 异常在代码下方 -

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) {

            }
        })
    }

这是我的例外:

尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)”

Android Kotlin NullReferenceException

评论

2赞 Egor 9/14/2022
在哪里初始化?当您尝试使用它时,它是空的。sub_recyclerview_videossetAdapter()
0赞 Oleg Skidan 9/14/2022
看起来您正在使用已弃用的 kotlin 扩展。
0赞 Andrey 9/15/2022
@Egor My 在 my 和 my 中,我的函数也在调用它。.sub_recyclerview_videosfragment_subscriptions.xmlSubscriptionsFragmentfetchVideosreturn inflater.inflate(R.layout.fragment_subscriptions, container, false)

答:

0赞 Andrey 9/15/2022 #1

问题不在于适配器或我的布局,实际上是我的类。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)
        }
    }