title: How to get an error message when data could not be written into the Firebase Database using Android
body: <p>I am using Android and I would like to store some data in the Firebase Realtime Database. Strangely, sometimes this works and sometimes it does not. I thought that this might have something to do with my WLAN connection which is not always good, but even if the WLAN connection is good sometimes data can be written into the Firebase Database, and sometimes it just does not work.</p>
  <p>Anyway, what I would like to have is a message to the user that the data has not been written into the Firebase Database if this was the case in form of a toast. For this purpose I have the following Java Fragment:</p>
  <pre><code>package com.example.td.bapp;
  
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.text.InputType;
  import android.util.Log;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.EditText;
  import android.widget.Toast;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.fragment.app.Fragment;
  import androidx.fragment.app.FragmentManager;
  import androidx.navigation.Navigation;
  
  import com.example.td.bapp.databinding.TestFragmentBinding ;
  import com.google.android.gms.tasks.OnCompleteListener;
  import com.google.android.gms.tasks.Task;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.TimeZone;
  import java.util.concurrent.atomic.AtomicBoolean;
  
  
      public class TestFragment extends Fragment implements View.OnClickListener {
      
      
      
          @Override
          public void onCreate(@Nullable Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
          }
      
          @Nullable
      
          private TestFragmentBinding binding;
          @Override
          public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      
              binding = TestFragmentBinding.inflate(inflater, container, false);
      
      
              /*
              Set onClickListeners to the buttoms
               */
      
              binding.orderingButton.setOnClickListener(this);
      
              return binding.getRoot();
      
          }
      
          public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
              super.onViewCreated(view, savedInstanceState);
      
      
      
          }
      
          @Override
          public void onClick(View view) {
      
              if(view.getId() == R.id.ordering_button) {
      
      
                  /*
                  Write data into the Firebase DB
                   */
      
                  long currentTimeMillis = System.currentTimeMillis();
                  SimpleDateFormat sdf1 = new SimpleDateFormat(&quot;dd-MM-yy&quot;);
                  sdf1.setTimeZone(TimeZone.getTimeZone(&quot;Europe/Berlin&quot;));
                  SimpleDateFormat sdf2 = new SimpleDateFormat(&quot;HH-mm-ss&quot;);
                  sdf1.setTimeZone(TimeZone.getTimeZone(&quot;Europe/Berlin&quot;));
      
      
                  DatabaseReference rootRef = FirebaseDatabase.getInstance(&quot;https://drink-server-db-default-rtdb.europe-west1.firebasedatabase.app&quot;).getReference();
                  DatabaseReference ordersRef = rootRef.child(&quot;orders&quot;);
                  String id =&quot;table_&quot; + MainActivity.tableNumber + &quot;_order_&quot; + MainActivity.orderNumberOfTheSession + &quot;_date_&quot; + sdf1.format(new Date()) + &quot;_time_&quot; + sdf2.format(new Date());
                  String name1 = &quot;Test_1&quot;;
                  String name2 = &quot;Test_2&quot;;
                  FirebaseDBItem_Order currentOrder = new FirebaseDBItem_Order(name1, name2);
                  Log.e(&quot;LogTag&quot;, &quot;Before write into DB&quot;);
                  ordersRef.child(id).setValue(currentOrder).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;Void&gt; task) {
                          if (task.isSuccessful()) {
                              Log.e(&quot;dbTAG&quot;,  &quot;Data successfully written.&quot;);
                              int duration = Toast.LENGTH_LONG;
                              Toast toast = Toast.makeText(getContext(), getString(R.string.message_orderSubmittedSuccessfully), duration);
                              toast.setGravity(Gravity.CENTER, 0, 0);
                              toast.show();
                          }
                          else {
                              Log.e(&quot;dbTAG&quot;, task.getException().getMessage());
                              int duration = Toast.LENGTH_LONG;
                              Toast toast = Toast.makeText(getContext(), getString(R.string.message_orderSubmittedNotSuccessfully), duration);
                              toast.setGravity(Gravity.CENTER, 0, 0);
                              toast.show();
                          }
                      }
                  });
      
                  try {
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  Log.e(&quot;LogTag&quot;, &quot;After write into DB&quot;);
      
      
                  Navigation.findNavController(getView()).navigate(TestFragmentDirections.actionFRCocktailToFRMenu());
      
              }
      
      
          }
      
      
      }
  </code></pre>
  <p>If the data is written successfully into the Firebase Database the toast is displayed and I get the correct dbTAG and LogTag messages:</p>
  <p>E/LogTag: Before write into DB</p>
  <p>E/LogTag: After write into DB</p>
  <p>E/dbTAG: Data successfully written.</p>
  <p>However, when the data is not successfully written into the Firebase Database, I just don't get any message from the dbTAG of the onComplete method and no new entry can be found in the database. I just get the LogTag messages:</p>
  <p>E/LogTag: Before write into DB</p>
  <p>E/LogTag: After write into DB</p>
  <p>Now actually I have 2 questions:</p>
  <ol>
  <li>Why sometimes the data can be written into the Firebase Database and sometimes not and why this seems to happen &quot;randomly&quot;? Can you think of some reasons?</li>
  <li>More importantly: How can I get an error message when the data could not be written into the Firebase Database such that I can inform the user? And related to this: Why do I not get a <code>Log.e(&quot;dbTAG&quot;, task.getException().getMessage());</code> message when the data was not successfully written into the Firebase Database? So the else branch of the <code>onComplete</code> method is not executed when the data is not successfully stored.</li>
  </ol>
  <p>If you need any further information, please let me know.</p>