0

I am currently trying to check whether 2 values, saved in my Firebase database, are equal to or greater than the other. Although I'm having no problems getting the value's and creating a Toast which displays the correct values, when I put them in an 'if' statement, both value's come up 'null'. Is there any solution to this?

Here some code.

 public void onDataChange(DataSnapshot dataSnapshot) {                                              
 if (dataSnapshot.child("endTime").exists()) {                                                  
     Long timeOpened = dataSnapshot.child("timeOpened").child("time").getValue(Long.class);     
     Long serviceEnd = dataSnapshot.child("endTime").child("time").getValue(Long.class);        
     Toast.makeText(StoryPreset.this, timeOpened + " " + serviceEnd, Toast.LENGTH_LONG).show(); 
     if (timeOpened >= serviceEnd) {                                                            
         Toast.makeText(StoryPreset.this, "Values are not null.", Toast.LENGTH_LONG).show();
     } else {                                                                                   
         Toast.makeText(StoryPreset.this, " they are null.", Toast.LENGTH_LONG).show();
     }   

As I mentioned, the 'Toast' present in the above code displays both values fine. It is only when they are put into an if statement that they become null. Any help is greatly appreciated!

EDIT: just to be more clear, inside the 'if' statement, bith 'timeOpened' and 'serviceEnd' are highlighted in yellow & when hovered over, a popup states that they may produce NullPointerExceptions. When testing, I also made sure I tested when I knrw 'timeOpened' was greater than 'serviceEnd' of which I got the result 'they are null'

EDIT: This question has been marked as a duplicate of the question "What is a NullPointerException". Just to be clear, I understand what a NullPointerException is and why it occurs, however I cannot understand as to why I cannot reference and compare values within the Firebase Database without them becoming NullPointerExceptions, and how I can compare them without having the NullPointerException result.

  • 1
    What is causing you to think the values are null? Are you getting a NullPointerException? Your toast strings are misleading. The toast "they are null" will appear when both `timeOpened` and `serviceEnd` are non-null and `timeOpened < serviceEnd`. If either are null, you will not see the toast, you will get an NPE. – Bob Snyder Nov 04 '17 at 03:14
  • My bad! I have editted the question to be more clear – JavaStruggleIsReal Nov 04 '17 at 08:48

0 Answers0