-2

I have created a database Related App. When I Run My App gets automatically Stopped . In The Log cat It Show NullPointerException on Datachange .

Here is My Firebase Code:

@Override
protected void onStart() {
    super.onStart();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference ezzeearnRef = rootRef.child(firebaseAuth.getCurrentUser().getUid());
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            long points = dataSnapshot.child("Points").getValue(Long.class);
            tv.setText(String.valueOf(points));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };
    ezzeearnRef.addValueEventListener(eventListener);
}
Sneh Pandya
  • 7,230
  • 6
  • 32
  • 49
Shawon
  • 1
  • 1

1 Answers1

0

Hi I think you are getting datasnapshot as null then also you are going access value of datasnapshot please write following line and check,

Replace your following code:

long points = dataSnapshot.child("Points").getValue(Long.class);
            tv.setText(String.valueOf(points));

with following

if(dataSnapshot.exist()){
long points = dataSnapshot.child("Points").getValue(Long.class);
            tv.setText(String.valueOf(points));}

and try.

Dhaval Solanki
  • 4,093
  • 1
  • 21
  • 33