Ad

NullPointerException: While Calling A Method From Another Class

- 1 answer

I have this function that tries to call a method in an android activity using the below firebase addchildevent listener but I get a "NullPointer" error. I need help implementing this code so I can reach that method in my other kotlin class and call it with the proper context.

companion object {
     private fun checkAdmin(userId: String) {

            isAdmin = false

            val ref: DatabaseReference = firebaseDatabase!!.reference
                .child("administrators")
                .child(userId)

            childEventListener = ref.addChildEventListener(object : ChildEventListener {
                override fun onCancelled(p0: DatabaseError) {
                    Log.d("Cancelled", "FirebaseUtil cancel")
                }

                override fun onChildMoved(p0: DataSnapshot, p1: String?) {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                }

                override fun onChildChanged(p0: DataSnapshot, p1: String?) {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                }

                override fun onChildAdded(p0: DataSnapshot, p1: String?) {
                    isAdmin = true
                    val caller = ListActivity()
                    caller.showMenu()

                }

                override fun onChildRemoved(p0: DataSnapshot) {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                }
            })
        }

        fun detachListener(callerActivity: Activity?) {
            firebaseAuth!!.removeAuthStateListener(object : AuthStateListener {
                override fun onAuthStateChanged(p0: FirebaseAuth) {
                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                }
            })
        }
 }

And this is the android activity method I am trying to call from the kotlin class:

fun showMenu() {
        [email protected]()
    }

Error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.connect.systems.ng.travelmantics, PID: 7882
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at androidx.appcompat.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
        at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
        at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
        at androidx.appcompat.app.AppCompatActivity.invalidateOptionsMenu(AppCompatActivity.java:245)
        at com.connect.systems.ng.travelmantics.ListActivity.showMenu(ListActivity.kt:67)
        at com.connect.systems.ng.travelmantics.FirebaseUtil$Companion$checkAdmin$1.onChildAdded(FirebaseUtil.kt:87)
        at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:[email protected]@18.0.1:79)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:[email protected]@18.0.1:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:[email protected]@18.0.1:55)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7343)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:933)
Ad

Answer

I was able to resolve the problem using the following method:

Steps:

  1. Modify the checkAdmin function to accept two parameters, one a string like before and the second an activity of type ListActivity:

    private fun checkAdmin(userId: String, activity: ListActivity) {
    
        isAdmin = false
    
        val ref: DatabaseReference = firebaseDatabase!!.reference
            .child("administrators")
            .child(userId)
    
        childEventListener = ref.addChildEventListener(object : ChildEventListener {
            override fun onCancelled(p0: DatabaseError) {
                Log.d("Cancelled", "FirebaseUtil cancel")
            }
    
            override fun onChildMoved(p0: DataSnapshot, p1: String?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
    
            override fun onChildChanged(p0: DataSnapshot, p1: String?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
    
            override fun onChildAdded(p0: DataSnapshot, p1: String?) {
                isAdmin = true
                activity.showMenu(activity)
    
            }
    
            override fun onChildRemoved(p0: DataSnapshot) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
        })
    }
    
  2. Call this function passing in two arguments, one the string and the other an activity and cast the second to a ListActivity type:

    checkAdmin(userId, callerActivity as ListActivity)
    
  3. In the ListActivity class setup the showMenu function as thus:

    fun showMenu(activity: ListActivity) {
    
       activity.invalidateOptionsMenu()
    }
    

And the above setup I was able to access the ListActivity's showManu function.

Ad
source: stackoverflow.com
Ad