I'm currently making a project in which a user segregates their trash and in return, it gives them points. Using those points they can redeem awards. At the moment I'm having trouble how to display the points in Android Studio's TextView. This is my code. my database structure https://imgur.com/wOGfYOg I want the points to be displayed on my TextView which is on my dashboard https://imgur.com/zJjSNgO the problem occurs at the DocumentReference portion.
public class MainActivity extends AppCompatActivity {
private TextView powents;
FirebaseFirestore fStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button logout = findViewById(R.id.logout);
ImageView qrimage = findViewById(R.id.qrimage);
powents = findViewById(R.id.powents);
fStore = FirebaseFirestore.getInstance();
DocumentReference docref = fStore.collection("Users").document("Users");
docref.addSnapshotListener(this, (documentSnapshot, error) -> {
powents.setText(Math.toIntExact(documentSnapshot.getLong("Points")));
});
try {
BarcodeEncoder barcode = new BarcodeEncoder();
Bitmap bitmap = barcode.encodeBitmap(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getEmail(), BarcodeFormat.QR_CODE, 650, 650);
qrimage.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
logout.setOnClickListener(view -> {
FirebaseAuth.getInstance().signOut();
Toast.makeText(MainActivity.this, "Logged Out!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Login.class));
finish();
});
}
}
CodePudding user response:
The problem in your code seems to be at this line of code:
DocumentReference docref = fStore.collection("Users").document("Users");
When you're using the above reference, it means that you are trying to read a document that has an ID called Users
, which actually doesn't exist, since your screenshot shows that the document ID is [email protected]
. To solve this, simple change to above document reference to:
DocumentReference docref = fStore.collection("Users").document("[email protected]");
And your code should, as long as you have proper rules. Don't also forget to attach a failure listener as well, to see if something goes wrong.
CodePudding user response:
I Think documentSnapshot.getLong("Points") is null and that's the reason you get NPE(Null pointer exception) and you can't set value to textview