0

In my current project I have a section where a user essentially enters a key to get the data under that key in firebase. I am using the get method as it only needs to be accessed once. The get method has an on Success and on failure listener and I have the entire thing within a try catch block but for some reason the app still bugs out and does a weird like crash and refresh action when a key that's not in the realtime database is entered. I ran a debug and for some reason when it reaches the get method in this situation instead of going to the catch block it leaves the function completely.

fun joinFinalised(newEmail: String, code: String) {
    if (code.isEmpty()){
        Toast.makeText(this, "No code entered", Toast.LENGTH_SHORT).show()
    } else {
        try {
            fDBRef.child(newEmail.replace(".","~dot~")).get().addOnSuccessListener {
                if (code == it.child("Code").value as String){
                    email = it.child("Email").value as String
                    fDBRef.child(email.replace(".","~dot~")).setValue(newCrew)
                    displayData()
                }
            }.addOnFailureListener{
                Toast.makeText(this, "Join failed", Toast.LENGTH_SHORT).show()
            }
        } catch (e: Exception) {
            Toast.makeText(this, "Join failed", Toast.LENGTH_SHORT).show()
        }
    }
}

The error it gives is java.lang.NullPointerException: null cannot be cast to non-null type kotlin.String which points to the line of the if statement where I compare the code.

Icy
  • 17
  • 4
  • At first glance this code looks fine and seems to handle the non-existence of both the node itself and its properties. When an app crashes though, it writes an error message and stack trace to its logcat. Please find those, and add them to your question by clicking the `edit` link under it. Also see https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this and https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Frank van Puffelen Jan 27 '23 at 05:08

1 Answers1

0

I fixed the problem by adding a second try catch around the if statement where the error pointed to

Icy
  • 17
  • 4
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 29 '23 at 17:09