0

This is the error in logcat:

When i try to retrieve username to my activity profile I have this error:

java.lang.NullPointerException and the app crash the problem on this line of code

// binding.etUserName.setText(user!!.username)

storage = FirebaseStorage.getInstance()
storageRef = storage.reference
binding.imgBack.setOnClickListener {
    onBackPressed()
}
binding.btnSave.setOnClickListener {
    uploadImage()

    binding.progressBar.visibility = View.VISIBLE
}
binding.userImage.setOnClickListener {
    chooseImage()
}
        
var firebaseUser = FirebaseAuth.getInstance().currentUser!!

databaseReference =
    FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.uid)


   databaseReference.addValueEventListener(object : ValueEventListener {
       override fun onCancelled(error: DatabaseError) {
           //Toast.makeText(applicationContext, error.message, Toast.LENGTH_SHORT).show()
           throw error.toException()
       }

       override fun onDataChange(snapshot: DataSnapshot) {

            val user = snapshot.getValue(User::class.java)
               binding.etUserName.setText(user!!.username)

          if (user!!.imageuri == "") {
                  binding.userImage.setImageResource(R.drawable.goog)
               } else {
                    Glide.with(this@profile).load(user.imageuri).into(binding.userImage)
             }
         }
    })
}
    
     

This is my user class:

data class User(var uid:String="" ,var username:String ="" ,var imageuri:String="",var emailedt:String="",) {

}

This is my sign up class how my node will save in databse

fun saveuserToFirebaseDatabase(imageuri: String){
        var auth:FirebaseAuth= FirebaseAuth.getInstance()
        var users:FirebaseUser?=auth.currentUser

          val uid=  FirebaseAuth.getInstance().uid ?: ""
          val ref = FirebaseDatabase.getInstance().getReference("/Users/$uid")
       var username = binding.nameReg.text.toString()

          val user =
              User( uid,binding.nameReg.text.toString(), imageuri, binding.Email.text.toString())
          ref.setValue(user)
              .addOnSuccessListener {
                  Log.d("RegisterActivity", "Finalle we saved")


      }
    }
}

this how my node will save in firebase realtime

this is my realtime databse

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MR SHADOWS
  • 71
  • 1
  • 3
  • If the app crashes it writes an error message and stack trace to its logcat output. Find those please, and add them to your question. Also see https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – Frank van Puffelen Apr 08 '22 at 14:14
  • Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Apr 08 '22 at 14:15
  • Please edit your question and add the information Frank asked for, and please also respond with @. – Alex Mamo Apr 08 '22 at 14:16
  • @FrankvanPuffelen this is not help my my problem i cant retrive username from realtime databse and show in my activity profile – MR SHADOWS Apr 08 '22 at 14:30
  • @a_local_nobody no it not help me i have problem with the display username look at my code – MR SHADOWS Apr 08 '22 at 14:32
  • From that error message it looks like `user` is `null` when you call `user!!.username`, so the `getReference("Users").child(firebaseUser.uid)` path in your database might not contain a user object for the specific user that is logged in? – Frank van Puffelen Apr 08 '22 at 16:16
  • why doesn't it help you ? that link explains how to get a stack trace, from your stack trace you can easily determine what's going wrong – a_local_nobody Apr 08 '22 at 16:36
  • @a_local_nobody i know how to stack trace i have screenshot for the problem the problem in ondatachange it return null but there is user look at my noode in realtime database – MR SHADOWS Apr 09 '22 at 02:45
  • @FrankvanPuffelen no bro it contain user i logged in every user accouts but it still show me null i cant solve the problem i try everything for 3 weeks but sill not solving it – MR SHADOWS Apr 09 '22 at 02:48
  • If you print `uid`, what does it show? – Frank van Puffelen Apr 09 '22 at 04:31
  • @FrankvanPuffelen when i make Toast to uid but the uid give another uid not the uid of current user how i logged in – MR SHADOWS Apr 10 '22 at 04:36

0 Answers0