title: Checking if a particular value exists in the Firebase database
body: <p>I am making an Android application using <code>Firebase</code> realtime database. When a new user registers on my app, that user's data is saved in the Firebase database. </p>
  
  <p>A user has to provide the following details to register:</p>
  
  <ol>
  <li>Full Name</li>
  <li>Email </li>
  <li>Username</li>
  <li>Password</li>
  </ol>
  
  <h3>Database Structure</h3>
  
  <p><a href="https://i.stack.imgur.com/KPTaX.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KPTaX.png" alt="enter image description here"></a></p>
  
  <p>Whenever a new user tries to register, I have to make sure that each user's username is unique so I check the database if the <code>username</code> entered by the user already exists in the database or not.</p>
  
  <p>To do this, I wrote the following method:</p>
  
  <pre><code>private boolean usernameExists(String username) {
       DatabaseReference fdbRefer = FirebaseDatabase.getInstance().getReference("Users/"+username);
       return (fdbRefer != null);
  }
  </code></pre>
  
  <p>My logic behind this method is that if <code>getReference</code> method cannot find reference to the specified path, it will return null so that I can return whether <code>fdbRefer</code> is <code>null</code> or not. If <code>fdbRefer</code> is <code>null</code>, then it means that <code>username</code> doesn't exist in the database.</p>
  
  <h3>Problem</h3>
  
  <p>Problem with this method is that it always returns <code>true</code> whether the entered <code>username</code> exists in the database or not. Which led me to believe that <code>fdbRefer</code> is never <code>null</code>. </p>
  
  <p>This brings me to my question... </p>
  
  <h3>Question</h3>
  
  <p>What does <code>getReference</code> method return when it can't find the specified path in the <code>firebase</code> database and what's the correct way to check if the <code>username</code> already exists in the database or not?</p>