title: Android: App Crashes when trying to save data to Firebase Database
body: <p>I'm trying to save data to a Firebase DB. Really nothing that complicated, but I'm finding that my app crashes whenever I click the saveInfoButton. I'm pretty experienced with Firebase with Swift/iOS, but newer to Android. It doesn't seem like this would be a problem with Firebase.</p>
  
  <p>The specific message (when I click the button) is: "Restaurant_app has stopped, open app again."</p>
  
  <p><strong>activity_main_page.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/activity_main_page"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="16dp"
      android:paddingLeft="64dp"
      android:paddingRight="64dp"
      android:paddingTop="16dp"
      tools:context="com.example.test.restaurant_app.MainPage"&gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_centerVertical="true"
          android:layout_centerHorizontal="true"&gt;
  
          &lt;TextView
              android:text="Email"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:textAppearance="?android:attr/textAppearanceLarge"
              android:id="@+id/userEmail" /&gt;
  
          &lt;EditText
              android:layout_margin="15dp"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:inputType="textEmailAddress"
              android:hint="Enter your name"
              android:id="@+id/nameTextField"
              /&gt;
  
          &lt;EditText
              android:layout_margin="15dp"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:inputType="text"
              android:hint="address"
              android:id="@+id/addressTextField"
              /&gt;
          &lt;EditText
              android:layout_margin="15dp"
              android:id="@+id/ageTextField"
              android:hint="Age"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" /&gt;
          &lt;Button
              android:layout_margin="15dp"
              android:id="@+id/saveInfoButton"
              android:text="Save Information"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" /&gt;
          &lt;Button
              android:text="Logout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:id="@+id/logoutButton" /&gt;
      &lt;/LinearLayout&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>UserDetails.java</strong></p>
  
  <pre><code>package com.example.test.restaurant_app;
  
  public class UserDetails {
  
      public String name;
      public String address;
      public int age;
  
      public UserDetails(String name, String address, int age) {
          this.name = name;
          this.address = address;
          this.age = age;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getAddress() {
          return address;
      }
  
      public void setAddress(String address) {
          this.address = address;
      }
  
      public int getAge() {
          return age;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  }
  </code></pre>
  
  <p><strong>MainPage.java</strong></p>
  
  <pre><code>package com.example.test.restaurant_app;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.google.firebase.auth.FirebaseAuth;
  import com.google.firebase.auth.FirebaseUser;
  import com.google.firebase.database.DatabaseReference;
  
  public class MainPage extends AppCompatActivity implements View.OnClickListener {
  
      private FirebaseAuth mAuth;
      private FirebaseUser user;
      private TextView textViewEmail;
      private Button logoutButton;
      private DatabaseReference dbReference;
  
      private EditText editTextName, editTextAddress, editTextAge;
      private Button saveInfo;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
  
          mAuth = FirebaseAuth.getInstance();
  
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main_page);
  
          logoutButton = (Button) findViewById(R.id.logoutButton);
          textViewEmail = (TextView) findViewById(R.id.userEmail);
          editTextName = (EditText) findViewById(R.id.nameTextField);
          editTextAddress = (EditText) findViewById(R.id.addressTextField);
          editTextAge = (EditText) findViewById(R.id.ageTextField);
          saveInfo = (Button) findViewById(R.id.saveInfoButton);
  
          if (mAuth.getCurrentUser() == null) {
              finish();
              startActivity(new Intent(getApplicationContext(), LoginActivity.class));
          }
  
          FirebaseUser user = mAuth.getCurrentUser();
          String name = user.getEmail().toString().trim();
          String welcomeMessage = "Hello, " + name;
          textViewEmail.setText(welcomeMessage);
  
          logoutButton.setOnClickListener(this);
          saveInfo.setOnClickListener(this);
  
      }
  
      public void saveInfo() {
          String name = editTextName.getText().toString();
          String address = editTextAddress.getText().toString();
          int age = Integer.parseInt(editTextAge.getText().toString());
  
          UserDetails userInfo = new UserDetails(name, address, age);
  
          dbReference.child("Users").child(user.getUid()).setValue(userInfo);
  
          Toast.makeText(this, "Information Saved", Toast.LENGTH_LONG).show();
  
      }
  
      public void onClick(View view) {
          if (view == logoutButton) {
              finish();
              startActivity(new Intent(getApplicationContext(), LoginActivity.class));
          }
          if (view == saveInfo){
              saveInfo();
          }
          }
      }
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>03-15 11:33:41.516 4595-4595/com.example.test.restaurant_app E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.example.test.restaurant_app, PID: 4595
                                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
                                                                                           at com.example.test.restaurant_app.MainPage.saveInfo(MainPage.java:65)
                                                                                           at com.example.test.restaurant_app.MainPage.onClick(MainPage.java:77)
                                                                                           at android.view.View.performClick(View.java:5637)
                                                                                           at android.view.View$PerformClick.run(View.java:22429)
                                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  
  03-15 11:33:41.521 1605-1828/system_process W/ActivityManager:   Force finishing activity com.example.test.restaurant_app/.MainPage
  
  03-15 11:33:41.554 1326-2029/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
  
  03-15 11:33:41.557 1326-2029/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
  
  03-15 11:33:41.558 1326-2029/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
  
  03-15 11:33:41.559 1605-3137/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
  
  03-15 11:33:41.559 1605-3137/system_process D/OpenGLRenderer: Swap behavior 1
  
  03-15 11:33:41.560 1605-3137/system_process W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
  
  03-15 11:33:41.560 1605-3137/system_process D/OpenGLRenderer: Swap behavior 0
  
  03-15 11:33:42.027 1605-1619/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{7bfe46d u0 com.example.test.restaurant_app/.MainPage t644 f}
  
  03-15 11:33:46.545 2380-5115/com.google.android.gms W/PlatformStatsUtil: Could not retrieve Usage &amp; Diagnostics setting. Giving up.
  
  03-15 11:33:51.529 1605-1619/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!
  </code></pre>