title: requestFocus() giving null pointer exception
body: <p>I have an edit text in my activity and want to request focus on it. However, it always crashes upon launching the activity.</p>
  
  <p>Java code:</p>
  
  <pre><code>foodText = (EditText) findViewById(R.id.foodName);
  prepText = (EditText) findViewById(R.id.prepText);
  foodText.requestFocus();
  </code></pre>
  
  <p>Activity.xml:</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:id="@+id/activity_add_food"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.rcadit.foodgenie.AddFood"&gt;
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
                  android:orientation="vertical"
  style="@style/AppTheme"
          &gt;
  
  
          &lt;ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;
  
  
  
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"&gt;
              &lt;LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:background="@color/colorAccent"
                  android:transitionName="addFood"&gt;
  
  
                  &lt;EditText
                      android:id="@+id/foodName"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:hint="Food name"
                      android:textSize="32sp"
  android:inputType="textCapWords"
  android:textColor="@color/colorIcons"
                      android:layout_marginLeft="64dp"
                      android:layout_marginRight="64dp"
                      android:layout_marginBottom="32dp"
                      android:layout_marginTop="32dp"
                      android:textColorHint="@color/colorIcons"/&gt;
              &lt;/LinearLayout&gt;
  
              &lt;/LinearLayout&gt;
  
  
          &lt;/ScrollView&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:paddingLeft="@dimen/activity_horizontal_margin"&gt;
  
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="16dp"
  
              android:textSize="16sp"
  
              android:text="Hunger Level" /&gt;
          &lt;Spinner
              android:id="@+id/hunger_spinner"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
  
  
              android:padding="16dp"
  
              &gt;
  
          &lt;/Spinner&gt;
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="16dp"
              android:textSize="16sp"
              android:text="Cuisine" /&gt;
          &lt;Spinner
              android:id="@+id/cuisine_spinner"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
  
  
              android:padding="16dp"
  
              &gt;
  
          &lt;/Spinner&gt;
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="16dp"
              android:textSize="16sp"
              android:text="Preparation Time" /&gt;
          &lt;EditText
              android:id="@+id/prepText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="16sp"
              android:padding="16dp"
              android:hint="Enter Preparation Time"/&gt;
  
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="16dp"
              android:textSize="16sp"
              android:text="Course" /&gt;
          &lt;Spinner
              android:id="@+id/course_spinner"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
  
  
              android:padding="16dp"
  
              &gt;
  
          &lt;/Spinner&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Salty"
              android:padding="16dp"
              android:textSize="16sp"
              /&gt;
          &lt;TextView
              android:id="@+id/howSalty"
              android:hint="Salty"
              android:textSize="12sp"
              android:paddingBottom="8dp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              /&gt;
  
          &lt;SeekBar
              android:id="@+id/salty"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:thumbTint="@color/colorAccent"
              android:progressTint="@color/colorAccent"
              android:max="2"
              android:progress="1"
              /&gt;
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Sweet"
              android:padding="16dp"
              android:textSize="16sp"/&gt;
          &lt;TextView
              android:id="@+id/howSweet"
              android:hint="Sweet"
              android:textSize="12sp"
              android:paddingBottom="8dp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"/&gt;
          &lt;SeekBar
              android:thumbTint="@color/colorAccent"
              android:progressTint="@color/colorAccent"
              android:id="@+id/sweet"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:max="2"
              android:progress="1"
              /&gt;
  
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Spicy"
              android:padding="16dp"
              android:textSize="16sp"/&gt;
  
          &lt;TextView
              android:id="@+id/howSpicy"
              android:hint="Spicy"
              android:textSize="12sp"
              android:paddingBottom="8dp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              /&gt;
          &lt;SeekBar
              android:thumbTint="@color/colorAccent"
              android:progressTint="@color/colorAccent"
              android:id="@+id/spicy"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:max="2"
              android:progress="1"
  
              /&gt;
  
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
  
              android:padding="16dp"
              android:text="Special Requirements"
  
              android:textSize="16sp" /&gt;
  
          &lt;GridLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
  
              android:layout_margin="16dp"
              android:columnCount="2"
              android:rowCount="3"&gt;
  
  
  
              &lt;CheckBox
                  android:layout_width="wrap_content"
                  android:id="@+id/dairyFree"
                  android:buttonTint="@color/colorAccent"
                  android:layout_height="wrap_content"
                  android:text="Dairy Free"
                  android:textSize="16sp"
                  android:onClick="onCheckboxClicked"/&gt;
  
              &lt;CheckBox
                  android:layout_width="wrap_content"
                  android:id="@+id/nutFree"
                  android:buttonTint="@color/colorAccent"
                  android:layout_height="wrap_content"
                  android:text="Nut Free"
                  android:textSize="16sp"
                  android:onClick="onCheckboxClicked"/&gt;
  
              &lt;CheckBox
                  android:id="@+id/eggFree"
                  android:layout_width="wrap_content"
                  android:buttonTint="@color/colorAccent"
                  android:layout_height="wrap_content"
                  android:text="Egg Free"
                  android:textSize="16sp"
                  android:onClick="onCheckboxClicked"
                  /&gt;
  
              &lt;CheckBox
                  android:id="@+id/vegetarian"
                  android:layout_width="wrap_content"
                  android:buttonTint="@color/colorAccent"
                  android:layout_height="wrap_content"
                  android:text="Vegetarian"
                  android:textSize="16sp"
                  android:onClick="onCheckboxClicked"/&gt;
          &lt;/GridLayout&gt;
          &lt;Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
  
              android:layout_margin="16dp"
              android:onClick="add"
  
              android:text="Add" /&gt;
      &lt;/LinearLayout&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>I have looked at other questions but I can't see why my code doesn't work as the ID's are correct and I can't find the fault in the code.</p>
  
  <p>Log file:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                    Process: com.example.rcadit.foodgenie, PID: 3221
                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rcadit.foodgenie/com.example.rcadit.foodgenie.AddFood}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.EditText.requestFocus()' on a null object reference
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                        at android.app.ActivityThread.-wrap11(ActivityThread.java)
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                        at android.os.Handler.dispatchMessage(Handler.java:102)
                        at android.os.Looper.loop(Looper.java:148)
                        at android.app.ActivityThread.main(ActivityThread.java:5417)
                        at java.lang.reflect.Method.invoke(Native Method)
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.EditText.requestFocus()' on a null object reference
                        at com.example.rcadit.foodgenie.AddFood.onCreate(AddFood.java:60)
                        at android.app.Activity.performCreate(Activity.java:6237)
                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:148) 
                        at android.app.ActivityThread.main(ActivityThread.java:5417) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
  I/Process: Sending signal. PID: 3221 SIG: 9
  Application terminated.
  </code></pre>
  
  <p>What's wrong?</p>