title: Getting a null pointer exception on a number picker - Android
body: <p>I am trying to do a simple "number picker" function that was custom made (<strong>not</strong> using the NumberPicker widget), by aligning 3 of them side by side. I got one to work just fine, but as soon as I added the other 2, then tried to change variable names (and adjusted it in the <code>MainActivity</code>), the program crashed with a <code>NullPointer Exception</code>. It is saying <code>line 29</code> is the culprit (<code>caused by</code>) in the <code>MainActivity</code>.</p>
  
  <p>I probably don't understand the MainActivity code very well, as it was code I borrowed. Why is it crashing? Thanks.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package com.azurespot.numbpicksample;
  
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  import android.app.Activity;
  
  public class MainActivity extends Activity implements OnClickListener {
      Button btnUp, btnDown;
      TextView textViewUp, textViewMid, textViewBottom;
  
      int nStart = 0;
      int nEnd = 59;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          btnUp = (Button) findViewById(R.id.buttonUp1|R.id.buttonUp2|R.id.buttonUp3);
          btnDown = (Button) findViewById(R.id.buttonDown1|R.id.buttonDown2|R.id.buttonDown3);
  
          // All the numbers
          textViewUp = (TextView) findViewById(R.id.plus1|R.id.plus2|R.id.plus3);
          textViewMid = (TextView) findViewById(R.id.mainNumber1|R.id.mainNumber2|R.id.mainNumber3);
          textViewBottom = (TextView) findViewById(R.id.minus1|R.id.minus2|R.id.minus3);
  
          textViewUp.setText("1"); // Line 29
          textViewMid.setText("0");
          textViewBottom.setText("59");
  
          btnUp.setOnClickListener(this);
          btnDown.setOnClickListener(this);
  
      }
  
      @Override
      public void onClick(View v) {
          String getString = String.valueOf(textViewMid.getText());
          int current = Integer.parseInt(getString);
          if (v == btnUp) {
              if (current &lt; nEnd) {
                  current++;
                  textViewUp.setText(String.valueOf(current - 1));
                  textViewMid.setText(String.valueOf(current));
                  textViewBottom.setText(String.valueOf(current + 1));
              }
  
          }
          if (v == btnDown) {
              if (current &gt; nStart) {
                  current--;
                  textViewUp.setText(String.valueOf(current - 1));
                  textViewMid.setText(String.valueOf(current));
                  textViewBottom.setText(String.valueOf(current + 1));
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center"
      android:orientation="horizontal" &gt;
  
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="left"
          android:orientation="vertical" &gt;
  
          &lt;Button
              android:id="@+id/buttonUp1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:background="@drawable/up_btn" /&gt;
  
          &lt;TextView
              android:id="@+id/plus1"
              android:layout_width="43dp"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:text="plus"
              android:textColor="#cc8400"
              android:textSize="16sp" /&gt;
  
          &lt;View
              android:id="@+id/topBar1"
              android:layout_width="30dp"
              android:layout_height="1dp"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:layout_marginTop="10dp"
              android:background="#000000" /&gt;
  
          &lt;TextView
              android:id="@+id/mainNumber1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:text="Main"
              android:textColor="#000000"
              android:textSize="30sp" /&gt;
  
          &lt;View
              android:id="@+id/lowerBar1"
              android:layout_width="30dp"
              android:layout_height="1dp"
              android:layout_marginBottom="5dp"
              android:layout_gravity="center_horizontal"
              android:background="#000000" /&gt;
  
          &lt;TextView
              android:id="@+id/minus1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:text="minus"
              android:textColor="#cc8400"
              android:textSize="16sp" /&gt;
  
          &lt;Button
              android:id="@+id/buttonDown1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:background="@drawable/down_btn" /&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;!-- start center numberpicker --&gt;
  
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:orientation="vertical" &gt;
  
          &lt;Button
              android:id="@+id/buttonUp2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:background="@drawable/up_btn" /&gt;
  
          &lt;TextView
              android:id="@+id/plus2"
              android:layout_width="43dp"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:text="plus"
              android:textColor="#cc8400"
              android:textSize="16sp" /&gt;
  
          &lt;View
              android:id="@+id/topBar2"
              android:layout_width="30dp"
              android:layout_height="1dp"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:layout_marginTop="10dp"
              android:background="#000000" /&gt;
  
          &lt;TextView
              android:id="@+id/mainNumber2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:text="Main"
              android:textColor="#000000"
              android:textSize="30sp" /&gt;
  
          &lt;View
              android:id="@+id/lowerBar2"
              android:layout_width="30dp"
              android:layout_height="1dp"
              android:layout_marginBottom="5dp"
              android:layout_gravity="center_horizontal"
              android:background="#000000" /&gt;
  
          &lt;TextView
              android:id="@+id/minus2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:text="minus"
              android:textColor="#cc8400"
              android:textSize="16sp" /&gt;
  
          &lt;Button
              android:id="@+id/buttonDown2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:background="@drawable/down_btn" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;!-- start right numberpicker --&gt;
  
      &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:orientation="vertical" &gt;
  
          &lt;Button
              android:id="@+id/buttonUp3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:background="@drawable/up_btn" /&gt;
  
          &lt;TextView
              android:id="@+id/plus3"
              android:layout_width="43dp"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:text="plus"
              android:textColor="#cc8400"
              android:textSize="16sp" /&gt;
  
          &lt;View
              android:id="@+id/topBar3"
              android:layout_width="30dp"
              android:layout_height="1dp"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:layout_marginTop="10dp"
              android:background="#000000" /&gt;
  
          &lt;TextView
              android:id="@+id/mainNumber3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:text="Main"
              android:textColor="#000000"
              android:textSize="30sp" /&gt;
  
          &lt;View
              android:id="@+id/lowerBar3"
              android:layout_width="30dp"
              android:layout_height="1dp"
              android:layout_marginBottom="5dp"
              android:layout_gravity="center_horizontal"
              android:background="#000000" /&gt;
  
          &lt;TextView
              android:id="@+id/minus3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:layout_marginBottom="5dp"
              android:text="minus"
              android:textColor="#cc8400"
              android:textSize="16sp" /&gt;
  
          &lt;Button
              android:id="@+id/buttonDown3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:background="@drawable/down_btn" /&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>LogCat</strong></p>
  
  <pre><code>FATAL EXCEPTION: main
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.azurespot.numbpicksample/com.azurespot.numbpicksample.MainActivity}: java.lang.NullPointerException
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
              at android.app.ActivityThread.access$600(ActivityThread.java:141)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:5041)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NullPointerException
              at com.azurespot.numbpicksample.MainActivity.onCreate(MainActivity.java:29)
              at android.app.Activity.performCreate(Activity.java:5104)
              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
              at android.app.ActivityThread.access$600(ActivityThread.java:141)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:5041)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
  </code></pre>