0

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://i.stack.imgur.com/TU5mM.jpg I want the points to be displayed on my TextView which is on my dashboard https://i.stack.imgur.com/9hfzb.jpg 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();
    });

}

}

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kersey
  • 31
  • 5
  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Mar 11 '22 at 12:35
  • I'm sorry, but you did not answer any of my questions. – Alex Mamo Mar 11 '22 at 12:37
  • @AlexMamo Hello! this code part of the code DocumentReference docref = fStore.collection("Users").document("Users"); docref.addSnapshotListener(this, (documentSnapshot, error) -> { powents.setText(Math.toIntExact(documentSnapshot.getLong("Points"))); Everytime I log in the user it shows the dashboard https://imgur.com/zJjSNgO then it crashes. – Kersey Mar 11 '22 at 12:39
  • 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 Mar 11 '22 at 12:40
  • @a_local_nobody unfortunately no. The problem is I don't know how to display an integer(which is from firebasefirestore) in android studio's textview. – Kersey Mar 11 '22 at 12:43
  • Please edit your question and add your database structure as a screenshot and indicate the exact data you want to read. – Alex Mamo Mar 11 '22 at 12:53
  • @AlexMamo copy that sorry for the confusion. – Kersey Mar 11 '22 at 12:59
  • `Everytime I log in the user it shows the dashboard then it crashes` you misunderstood what i sent you then, the link i provided you shows how to get your stack trace, with that stack trace you'll understand _why_ it is crashing, if you research why that happens you can fix your problems without having to ask here for help :) – a_local_nobody Mar 13 '22 at 09:17

2 Answers2

1

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 user1@gmail.com. To solve this, simple change to above document reference to:

DocumentReference docref = fStore.collection("Users").document("user1@gmail.com");

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Oh my goodness it finally worked! But should I set this one to Email instead of user1@gmail.com since this will be used "theoretically" by different users. – Kersey Mar 11 '22 at 13:25
  • Yes, you should instead use the UID instead of the email address. Why? Because a user can change the email address while the UID will always remain the same. – Alex Mamo Mar 11 '22 at 13:35
  • Got it! And my apologies it's my first time posting here so my bad on not clicking that one sooner. Cheers! – Kersey Mar 11 '22 at 13:40
0

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

OneDev
  • 557
  • 3
  • 14
  • Oh I see I'm still a bit confused as to how I'm going to go around that issue since during registration of the user I set a default points number which is 0. – Kersey Mar 11 '22 at 13:15