I have an 'if' statement for checking null pointer exception but that line itself is giving a null pointer excpetion

Multi tool use
Multi tool use


I have an 'if' statement for checking null pointer exception but that line itself is giving a null pointer excpetion



Code


if (dataSnapshot.child("Email").getValue().toString()==null) {
Email.setText("Email not found");
} else {
Email.setText(dataSnapshot.child("Email").getValue().toString());
}

if (dataSnapshot.child("Quote").getValue().toString()!=null) {
Email.setText(dataSnapshot.child("Quote").getValue().toString());
} else {
Email.setText("Quote not found");
}



The line of the if statement is itself showing an error. How do I solve this problem?


if





May be your dataSnapshot is null or dataSnapshot.child("Email").getValue() returning null value.
– Lovekush Vishwakarma
Jun 30 at 8:42





It's not funny. You are not considering all the possible situations. Read above comment.
– Ranjan
Jun 30 at 8:43





can you post your LogCat where you are getting the exception? because in your if statement there are various parts where can throw nullPointerExceprion
– SepJaPro2.4
Jun 30 at 8:57


LogCat


if


nullPointerExceprion




3 Answers
3



Here is how you can rewrite it:


if(dataSnapshot != null && dataSnapshot.hasChild("Email") && dataSnapshot.child("Email").getValue() != null ){
//Your code...
}



You have to check for all possibilities.





Can you add your dataSnapshot value in the question
– mark922
Jun 30 at 10:00



You're checking if the toString() value is null. Only that value.


toString()



You're not checking any of the values that precedes it.



dataSnapshot could be null.
dataSnapshot.child("Email") could be null.
dataSnapshot.child("Email").getValue() could be null.


dataSnapshot


dataSnapshot.child("Email")


dataSnapshot.child("Email").getValue()



Take each value, save it to a variable, and do a null check on ALL those values.



This is the only way to do it in Java.





No i checked... its not
– Emilia James
Jun 30 at 9:48





I call bullshit. 100% sure. Post the actual exception if you have a crash. Otherwise you'll have us guessing forever without getting any help.
– Moonbloom
Jun 30 at 10:01





And as you just accepted an answer that states exactly what i said, my 'bullshit calling' was correct.
– Moonbloom
Jun 30 at 10:06





It was just the logic man... i mean i had myself told that the database was null
– Emilia James
Jun 30 at 10:09





like the child didn't exist at all...
– Emilia James
Jun 30 at 10:09


if (dataSnapshot.child("Email").getValue().toString()==null) {

}



install a breakpoint before if and debug it. I think either dataSnapshot is null or dataSnapshot.child("Email") is resulting null which is causing err.


dataSnapshot


dataSnapshot.child("Email")





i did... its the value which is null in database
– Emilia James
Jun 30 at 9:48






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.

daUFMTKaylkHZgaL2 mY6hV44Cgwk1l0SWKqSdF5DeOtF 4ARs9
OO3lhlkp1PHm0RmTAbpRPd,lzbP9Ltg3PbhWA4kd8agi Q Y26BY8uIfA pbutqnQrVRb

Popular posts from this blog

Delphi Android file open failure with API 26

.

Amasya