I have some data in Firebase and want to retrieve it. Sometimes it can be null. How do I not get an exception for it?
I have some data in Firebase and want to retrieve it. Sometimes it can be null. How do I not get an exception for it?
FetchDataMethod
private void fetchData() {
mDatabaseReference.child("UserData").child(RecieversId).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ImageView Image = (ImageView) findViewById(R.id.imageView);
TextView Name = (TextView) findViewById(R.id.name);
TextView Status = (TextView) findViewById(R.id.status);
TextView Email = (TextView) findViewById(R.id.email);
TextView Ph = (TextView) findViewById(R.id.ph);
TextView Quote = (TextView) findViewById(R.id.quote);
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
String image = postSnapshot.child("Image").getValue().toString();
String name = postSnapshot.child("Name").getValue().toString();
String status = postSnapshot.child("Status").getValue().toString();
String email = postSnapshot.child("Email").getValue().toString();
String ph = postSnapshot.child("Phone_Number").getValue().toString();
String quote = postSnapshot.child("Quote").getValue().toString();
Name.setText(name);
Status.setText(status);
Email.setText(email);
Ph.setText(ph);
Quote.setText(quote);
Image.setImageResource(Integer.parseInt(image));
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
How do I use the if statement? Or is there anyway to achieve the same without an if statement?
if
if
Database structure:
https://ibb.co/fLe8gd
Code:
public void onDataChange(DataSnapshot dataSnapshot) {
ImageView Image = (ImageView) findViewById(R.id.imageView);
TextView Name = (TextView) findViewById(R.id.name);
TextView Status = (TextView) findViewById(R.id.status);
TextView Email = (TextView) findViewById(R.id.name);
TextView Ph = (TextView) findViewById(R.id.number);
TextView Quote = (TextView) findViewById(R.id.name);
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
if (dataSnapshot != null) {
// String image = postSnapshot.child("Image").getValue().toString();
String name = dataSnapshot.child("Name").getValue().toString();
String status = dataSnapshot.child("Status").getValue().toString();
String email = dataSnapshot.child("Email").getValue().toString();
String ph = dataSnapshot.child("Phone_Number").getValue().toString();
String quote = dataSnapshot.child("Quote").getValue().toString();
Name.setText(name);
Email.setText(email);
Status.setText(status);
Ph.setText(ph);
Quote.setText(quote);
// Image.setImageResource(Integer.parseInt(image));
}
else {
Status.setText("Status not found");
Email.setText("Email not found");
Quote.setText("Quote not found");
}
}
I tried this but this isn't working. I tried datasnapshot.exists too. I don't know what else to do.
Yes id is not nulll
– Emilia James
Jun 30 at 7:48
2 Answers
2
try {
private void fetchData() { ... }
catch (Exception exception){
Log.d(TAG, "fetchData: " + exception);
}
I know it will show an exception... i just want to know how to not get an exception using IF method... because i want to set data for null data so try catch wont be useful
– Emilia James
Jun 30 at 8:09
Does the null occur on all the values or specific ones? Before setText() of each you could try e.g. if(image.equals(null) { //Do something }
– Malcolm Maima
Jun 30 at 8:15
The null occurs in the line which the data is empty...i know it is empty but i want it to ignore it... like if the data is empty i want the textview to display something else
– Emilia James
Jun 30 at 8:18
private void fetchData() {
mDatabaseReference.child("UserData").child(RecieversId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ImageView Image = (ImageView) findViewById(R.id.imageView);
TextView Name = (TextView) findViewById(R.id.name);
TextView Status = (TextView) findViewById(R.id.status);
TextView Email = (TextView) findViewById(R.id.email);
TextView Ph = (TextView) findViewById(R.id.number);
TextView Quote = (TextView) findViewById(R.id.quote);
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
if (dataSnapshot!=null && dataSnapshot.hasChild("Email") && dataSnapshot.child("Email") != null) {
Email.setText(dataSnapshot.child("Email").getValue().toString());
} else {
Email.setText("Email not found");
}
if (dataSnapshot!=null && dataSnapshot.hasChild("Email") && dataSnapshot.child("Quote").getValue() != null) {
Email.setText(dataSnapshot.child("Email").getValue().toString());
} else {
Quote.setText("Quote not found");
}
{
// String image = postSnapshot.child("Image").getValue().toString();
String name = dataSnapshot.child("Name").getValue().toString();
String status = dataSnapshot.child("Status").getValue().toString();
String ph = dataSnapshot.child("Phone_Number").getValue().toString();
Name.setText(name);
Status.setText(status);
Ph.setText(ph);
// Image.setImageResource(Integer.parseInt(image));
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Finally figured out the answer!...
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are u retrieving receiver Id using other snapshot ?
– Raj
Jun 30 at 7:26