0

I am using firebase realtime database in my application. If the data I send with Intent is empty, the application closes. If the data in the intent is empty, how can I connect to the database and pull data?

String post_title ;

post_title = getIntent().getExtras().get("post_title").toString();
txttitle.setText(post_title);

if post_title is null i want it to do this:

databaseReference = FirebaseDatabase.getInstance().getReference().child("AllPost").child(PostKey);

databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.hasChild("title")){                          
            String title  = dataSnapshot.child("title").getValue().toString();
                          
            txttitle.setText(title);
        }

log :enter image description here

log: enter image description here

I tried this:

  if (post_title == null || post_title.isEmpty()) {
            databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if (snapshot.hasChild("title")) {
                        String title = snapshot.child("title").getValue().toString();

                        txttitle.setText(title);
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        } else {
            
            txttitle.setText(post_title);
           
        }
  • Have you tried doing `if (post_title == null || post_title.isEmpty()) { /* do firebase operations */}` – Darshan Jun 10 '22 at 18:20
  • When an app closes it typically crashed, and it writes an error message and stack trace to its logcat when that happens. 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 Jun 10 '22 at 18:34
  • Please edit your question and add the information Frank asked for, and please also respond with @. – Alex Mamo Jun 11 '22 at 08:44
  • Yes, I tried. It gave the same error again. I added log @FrankvanPuffelen – Muratcan Yıldız Jun 11 '22 at 12:25
  • It's incomplete. Please edit your question and add the entire error message and stack trace. – Alex Mamo Jun 11 '22 at 12:29
  • ı added log 146. now @AlexMamo – Muratcan Yıldız Jun 11 '22 at 12:38
  • I'm not sure I understand. I can only see the old (incomplete) screenshot. – Alex Mamo Jun 11 '22 at 12:43
  • It sounds like there is no `post_title` in the extras of the intent. I hope you are aware that data is loaded from Firebase (and most modern cloud APIs) asynchronously. https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Jun 11 '22 at 12:52
  • Thank you your answer . If the user comes to the postactiviy page from the notifications page, extras are not available. That was my question. Is there anything I can do to make the empty intent not close the app? – Muratcan Yıldız Jun 11 '22 at 13:11
  • Yes, you can check if it is null or empty before using it. If you have tried that, update the question to show what specifically you tried. – Tyler V Jun 11 '22 at 13:41
  • i added what i tried @TylerV – Muratcan Yıldız Jun 12 '22 at 11:25

1 Answers1

0

Your problem is in this line

post_title = getIntent().getExtras().get("post_title").toString();

When "post_title" does not exist, get("post_title") returns null. Then you get a NullPointerException because you call toString() on it. You can also see this in the error message

Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object

The fix for this is to add some null checks where you extract post_title to guard against 1) having no extras (Bundle being null), 2) having no post_title entry, and 3) having a post_title entry that cannot be cast to a string.

This would look like:

String post_title = "";
Bundle b = getIntent().getExtras();
if( b != null ) {
    post_title = b.getString("post_title");
}

if( post_title == null || post_title.isEmpty() ) {
    // call firebase
}
else {
    txttitle.setText(post_title);
}

Alternately, you can just use getStringExtra, which will do the same checks internally for you, and return null if the extras are missing or the post_title entry is missing or not a String.

String post_title = getIntent().getStringExtra("post_title");

if( post_title == null || post_title.isEmpty() ) {
    // call firebase
}
else {
    txttitle.setText(post_title);
}
Tyler V
  • 9,694
  • 3
  • 26
  • 52