Home > Software design >  If getIntent is null Firebase
If getIntent is null Firebase

Time:06-13

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);
           
        }

CodePudding user response:

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);
}
  • Related