title: Google Sign-In button causing null object reference
body: <p>I'm trying to implement a Google Sign In button and getting a null object reference error when I call <code>signInButton.setSize()</code>. I initialize the button with <code>findViewById()</code>. I do it in <code>promptSignIn()</code> which is a couple stack levels higher than <code>onCreate()</code>. I tried initializing in <code>onCreate()</code> as other answers suggest, and I get the same error.</p>
  
  <p><strong>YourOrdersActivity.java</strong></p>
  
  <pre><code>package com.example.mattf.yankeetoyboxv1;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  
  import com.google.android.gms.auth.api.signin.GoogleSignIn;
  import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  import com.google.android.gms.auth.api.signin.GoogleSignInClient;
  import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  import com.google.android.gms.common.SignInButton;
  import com.google.android.gms.tasks.Task;
  
  /**
   * Created by mattf on 12/8/2017.
   */
  
  public class SignInActivity extends BaseActivity{
  
      private GoogleSignInClient mGoogleSignInClient;
      private SignInButton signInButton;
      private static int RC_SIGN_IN=100;
      private static final String TAG="SignInActivity";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          isUserSignedIn();
      }
  
      @Override
      protected void onStart() {
          super.onStart();
  
          isUserSignedIn();
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode,resultCode,data);
  
          //Result return from launching the Intent from GoogleSignInClient.getSignInIntent(...);
          if (requestCode == RC_SIGN_IN) {
              // The Task returned from this call is always completed
              // no need to attach a listener
              Task&lt;GoogleSignInAccount&gt; task = GoogleSignIn.getSignedInAccountFromIntent(data);
              handleSignInResult(task);
          }
      }
  
      private void isUserSignedIn() {
          GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
  
          if (account==null){
              promptSignIn();
          }
          else updateUI(account);
      }
  
      private void promptSignIn() {
          // Configure sign-in to request the user's ID, email address, and basic profile
          // ID and basic profile are included in DEFAULT_SIGN_IN
          GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
  
          // Build a GoogleSignInClient with the options specified by gso
          mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
  
          // Set the dimensions of the sign-in button
          signInButton = findViewById(R.id.sign_in_button);
          signInButton.setSize(SignInButton.SIZE_STANDARD);
          signInButton.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View view) {
                  signIn();
              }
          });
      }
  
      private void signIn() {
          Intent signInIntent = mGoogleSignInClient.getSignInIntent();
          startActivityForResult(signInIntent,RC_SIGN_IN);
      }
  
      private void handleSignInResult(Task&lt;GoogleSignInAccount&gt; completedTask) {
          try {
              GoogleSignInAccount account = completedTask.getResult();
  
              // Signed in successfully, show authenticated UI
              updateUI(account);
  
          } catch (Exception e) {
              Log.w(TAG, "signInResult:failed code=" + e.getMessage());
          }
      }
  
      protected void updateUI(GoogleSignInAccount account) {
          if (signInButton != null) signInButton.setVisibility(View.GONE);
      }
  }
  </code></pre>
  
  <p>The button is in this XML file which is linked to the <code>YourOrdersActivity.java</code> activity.</p>
  
  <p><strong>your_orders_activity.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.mattf.yankeetoyboxv1.YourOrdersActivity"&gt;
  
      &lt;com.google.android.gms.common.SignInButton
          android:id="@+id/sign_in_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="16dp"
          android:layout_marginEnd="16dp"
          android:layout_marginStart="16dp"
          android:layout_marginTop="16dp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p><strong>LogCat</strong></p>
  
  <pre><code>12-08 20:31:51.624 12373-12373/com.example.mattf.yankeetoyboxv1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.mattf.yankeetoyboxv1, PID: 12373
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mattf.yankeetoyboxv1/com.example.mattf.yankeetoyboxv1.YourOrdersActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.SignInButton.setSize(int)' on a null object reference
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                                        at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                        at android.os.Looper.loop(Looper.java:164)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.SignInButton.setSize(int)' on a null object reference
                                                                                        at com.example.mattf.yankeetoyboxv1.SignInActivity.promptSignIn(SignInActivity.java:72)
                                                                                        at com.example.mattf.yankeetoyboxv1.SignInActivity.isUserSignedIn(SignInActivity.java:57)
                                                                                        at com.example.mattf.yankeetoyboxv1.SignInActivity.onCreate(SignInActivity.java:30)
                                                                                        at android.app.Activity.performCreate(Activity.java:7000)
                                                                                        at android.app.Activity.performCreate(Activity.java:6991)
                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                                        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                        at android.os.Looper.loop(Looper.java:164) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
  </code></pre>