title: Why are these two objects null?
body: <p>I'm writing an Android app and two buttons ("btnGPSShowLocation" and "button")  that I am using are null. I've instantiated them both so I don't know why they are null. Here is my code:</p>
  
  <pre><code>public class AndroidLocationActivity extends Activity {
  Button btnGPSShowLocation;
  Button button;
  
  
  AppLocationService appLocationService;
  private final static int INTERVAL = 1000 * 60 * 2; //2 minutes
  Handler mHandler = new Handler();
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      appLocationService = new AppLocationService(
              AndroidLocationActivity.this);
  
      btnGPSShowLocation = (Button) findViewById(R.id.btnGPSShowLocation);
  
      btnGPSShowLocation.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View arg0) {
  
              Location gpsLocation = appLocationService
                      .getLocation(LocationManager.GPS_PROVIDER);
  
              if (gpsLocation != null) {
                  double latitude = gpsLocation.getLatitude();
                  double longitude = gpsLocation.getLongitude();
                  Toast.makeText(
                          getApplicationContext(),
                          "Mobile Location (GPS): \nLatitude: " + latitude
                                  + "\nLongitude: " + longitude,
                          Toast.LENGTH_LONG).show();
              } else {
                  showSettingsAlert("GPS");
              }
  
          }
      });
  
      button = (Button) findViewById(R.id.button12);
  
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startActivity(new Intent("locationActivity"));
          }
      });
  } 
  </code></pre>
  
  <p>And here is the XML that may be relevant:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout 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"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"
  tools:context="it226.myapplicationit226androidapp.AndroidLocationActivity"
  tools:showIn="@layout/activity_gps"&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Show Location\n(GPS)"
      android:id="@+id/btnGPSShowLocation"
      android:layout_alignParentTop="true"
      android:layout_marginTop="83dp"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/nextgps"
      android:id="@+id/button12"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="106dp" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Edit: here is the logcat</p>
  
  <pre><code>&gt; 04-27 23:34:30.525 18310-18310/it226.myapplicationit226androidapp
  &gt; E/AndroidRuntime: FATAL EXCEPTION: main
  &gt;                                                                                     Process: it226.myapplicationit226androidapp, PID: 18310
  &gt;                                                                                     java.lang.RuntimeException: Unable to start activity
  &gt; ComponentInfo{it226.myapplicationit226androidapp/it226.myapplicationit226androidapp.AndroidLocationActivity}:
  &gt; java.lang.NullPointerException: Attempt to invoke virtual method 'void
  &gt; android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
  &gt; on a null object reference
  &gt;                                                                                         at
  &gt; android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
  &gt;                                                                                         at
  &gt; android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
  &gt;                                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)
  &gt;                                                                                         at
  &gt; android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
  &gt;                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
  &gt;                                                                                         at android.os.Looper.loop(Looper.java:148)
  &gt;                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
  &gt;                                                                                         at java.lang.reflect.Method.invoke(Native Method)
  &gt;                                                                                         at
  &gt; com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  &gt;                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  &gt;                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual
  &gt; method 'void
  &gt; android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
  &gt; on a null object reference
  &gt;                                                                                         at
  &gt; it226.myapplicationit226androidapp.AndroidLocationActivity.onCreate(AndroidLocationActivity.java:39)
  &gt;                                                                                         at android.app.Activity.performCreate(Activity.java:6237)
  &gt;                                                                                         at
  &gt; android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
  &gt;                                                                                         at
  &gt; android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
  &gt;                                                                                         at
  &gt; android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
  &gt;                                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
  &gt;                                                                                         at
  &gt; android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
  &gt;                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
  &gt;                                                                                         at android.os.Looper.loop(Looper.java:148) 
  &gt;                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
  &gt;  
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
  </code></pre>