title: DatabaseException on using getUid() function in Firebase Authentication
body: <p>I am trying to get the id of the logged in user and use it in two fragments. In the fragment I am using first, id gets retrieved and stored in my global variable properly but when I access the other fragment after that, my app crashes saying that getUid() is generating a null pointer exception in the other activity.</p>
  
  <p>Here is my code (Both fragments have identical codes except for different variable names. LDQApp is my class for global variables)</p>
  
  <pre><code>public class RecruitmentFragment extends Fragment {
  
      private DatabaseReference mDatabase;
  
      EditText name_text,reg_text,year_txt, mob_txt,interest_txt;
      Button button;
  
      public RecruitmentFragment() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.fragment_recruitment, container, false);
  
          name_text = (EditText)rootView.findViewById(R.id.editText2);
          reg_text = (EditText)rootView.findViewById(R.id.editText3);
          year_txt = (EditText)rootView.findViewById(R.id.editText4);
          mob_txt = (EditText)rootView.findViewById(R.id.editText5);
          interest_txt = (EditText)rootView.findViewById(R.id.editText6);
  
          button = (Button)rootView.findViewById(R.id.button2);
  
          FirebaseDatabase.getInstance().setPersistenceEnabled(true);
         mDatabase=FirebaseDatabase.getInstance().getReference("recruitment");
  
      FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
      LDQApp.uid = firebaseUser.getUid();
  
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              final String name,regNo,mobNo,interest;
              int year;
  
              name = name_text.getText().toString();
              regNo = reg_text.getText().toString();
              year = Integer.parseInt(year_txt.getText().toString());
              mobNo = mob_txt.getText().toString();
              interest = interest_txt.getText().toString();
  
              RecruitUser recruitUser = new RecruitUser(name,regNo,year,mobNo,interest);
  
              mDatabase.child(LDQApp.uid).setValue(recruitUser);
  
              mDatabase.child(LDQApp.uid).addValueEventListener(new ValueEventListener() {
                  @Override
                  public void onDataChange(DataSnapshot dataSnapshot) {
  
                      RecruitUser sUser = dataSnapshot.getValue(RecruitUser.class);
                      Toast.makeText(getContext(),"Submitted successfully",Toast.LENGTH_SHORT).show();
  
                      Log.d(TAG, "User name: " + name + ", Registration No: " + regNo);
                  }
  
                  @Override
                  public void onCancelled(DatabaseError error) {
                      // Failed to read value
                      Log.w(TAG, "Failed to read value.", error.toException());
                  }
              });
          }
      });
  
      return rootView;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is the log as requested</p>
  
  <p><a href="https://i.stack.imgur.com/bprZV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bprZV.png" alt="Log"></a></p>