Adit R Adit R - 8 days ago 5
Android Question

requestFocus() giving null pointer exception

I have an edit text in my activity and want to request focus on it. However, it always crashes upon launching the activity.

Java code:

foodText = (EditText) findViewById(R.id.foodName);
prepText = (EditText) findViewById(R.id.prepText);
foodText.requestFocus();


Activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
style="@style/AppTheme"
>


<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">



<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorAccent"
android:transitionName="addFood">


<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"/>
</LinearLayout>

</LinearLayout>


</ScrollView>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"

android:textSize="16sp"

android:text="Hunger Level" />
<Spinner
android:id="@+id/hunger_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"


android:padding="16dp"

>

</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"
android:text="Cuisine" />
<Spinner
android:id="@+id/cuisine_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"


android:padding="16dp"

>

</Spinner>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"
android:text="Preparation Time" />
<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"/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"
android:text="Course" />
<Spinner
android:id="@+id/course_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"


android:padding="16dp"

>

</Spinner>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Salty"
android:padding="16dp"
android:textSize="16sp"
/>
<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"
/>

<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"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sweet"
android:padding="16dp"
android:textSize="16sp"/>
<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"/>
<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"
/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Spicy"
android:padding="16dp"
android:textSize="16sp"/>

<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"
/>
<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"

/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:padding="16dp"
android:text="Special Requirements"

android:textSize="16sp" />

<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:layout_margin="16dp"
android:columnCount="2"
android:rowCount="3">



<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"/>

<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"/>

<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"
/>

<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"/>
</GridLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_margin="16dp"
android:onClick="add"

android:text="Add" />
</LinearLayout>

</android.support.constraint.ConstraintLayout>


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.

Log file:

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.


Can someone please tell me what's wrong?

Answer

So I was able to reproduce your error by not calling setContentView() before using your code. So the correct way to do it is to do:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activity);
        foodText = (EditText) findViewById(R.id.foodName);
        prepText = (EditText) findViewById(R.id.prepText);
        foodText.requestFocus();
}

your_activity is basically the name of your xml file without the xml extension. For more documentation on Views you can check this link or this link ifwhat you're doing is creating a second Activity. (both refer to the official documentation and give examples and explanations)

Also I suppose that you would get the same error if you don't use the right name of file for the setContentView() method.

Comments