0

I have a rare problem in an Android project with Java. It happens that when I start my application, it tells me:

"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setChecked (boolean)' on a null object reference"

But, everything is correct, the layout, the ID exists, everything is correct, the weird thing is, that it is only solved when I modify the activity, I give an enter or a space and it already works, but when I run it it already gives the same error in another activity and I have to be fixing like that all the time, it's frustrating. What could be generating this?

Example code MainActivity:

protected void onCreate(Bundle paramBundle) {
     setTheme(R.style.AppThemeDark);
    super.onCreate(paramBundle);
    setContentView(R.layout.activity_main);
 Switch switch_ = (Switch) findViewById(R.id.theme_switch);
    switch_.setChecked(this.isDark);
    this.themeSwitch = switch_;
}

Resource:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools"
    android:id="@id/drawer_layout" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
        <FrameLayout android:id="@id/fragment_container" android:layout_width="fill_parent" android:layout_height="fill_parent" />
    </RelativeLayout>
    <com.google.android.material.navigation.NavigationView android:layout_gravity="start" android:id="@id/nav_view" android:fitsSystemWindows="true" android:layout_width="wrap_content" android:layout_height="fill_parent">
        <androidx.core.widget.NestedScrollView android:layout_width="fill_parent" android:layout_height="fill_parent">
            <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
                <LinearLayout android:gravity="bottom" android:orientation="vertical" android:id="@id/nav_head_layout" android:background="@drawable/bg_gradient_nav_bg" android:layout_width="fill_parent" android:layout_height="130.0dip">
                    <ImageView android:layout_gravity="center" android:padding="5.0dip" android:layout_width="110.0dip" android:layout_height="80.0dip" android:src="@drawable/logo" />
                    <TextView android:textAppearance="@style/TextAppearance.AppCompat.Title" android:textSize="18.0sp" android:textColor="@color/white" android:gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10.0dip" android:text="@string/app_title" android:layout_marginStart="10.0dip" />
                </LinearLayout>
                <View android:background="@color/grey_transparent" android:layout_width="fill_parent" android:layout_height="1.0px" />
                <androidx.recyclerview.widget.RecyclerView android:id="@id/recyclerView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:nestedScrollingEnabled="false" />
                <View android:background="@color/grey_transparent" android:layout_width="fill_parent" android:layout_height="1.0px" />
                <RelativeLayout android:gravity="center_vertical" android:padding="10.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="8.0dip">
                    <ImageView android:gravity="center_vertical" android:id="@id/image" android:layout_width="20.0dip" android:layout_height="20.0dip" android:layout_marginStart="15.0dip" app:srcCompat="@drawable/ic_brightness_3_black_24dp" />
                    <TextView android:textSize="12.0sp" android:textStyle="bold" android:textColor="@color/default_text" android:gravity="start" android:id="@id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/dark_mode" android:layout_marginStart="20.0dip" android:layout_toEndOf="@id/image" />

                    <Switch android:gravity="center_vertical" android:id="@id/theme_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true"
                 />
                </RelativeLayout>
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>
    </com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Could you share some of the code at which this error occurs? – Ewan Brown Jan 07 '22 at 16:04
  • There I added an excerpt. – Fiorella Gamarra Aguado Jan 07 '22 at 16:07
  • Why are you calling setTheme before a super call, that is just inviting more random issues like this one to happen (not saying that is causing this issue though). – Ma3x Jan 07 '22 at 16:10
  • It does not solve the problem. :( – Fiorella Gamarra Aguado Jan 07 '22 at 16:15
  • Does [this](https://stackoverflow.com/questions/38290903/attempt-to-invoke-virtual-method-void-android-widget-switch-setcheckedboolean) answer your question? – Ewan Brown Jan 07 '22 at 16:16
  • Does this answer your question? [Attempt to invoke virtual method 'void android.widget.Switch.setChecked(boolean)' on a null object reference](https://stackoverflow.com/questions/38290903/attempt-to-invoke-virtual-method-void-android-widget-switch-setcheckedboolean) – Ewan Brown Jan 07 '22 at 16:17
  • I had already tried at the beginning, it does not work for me and it happens to me in all Activities (sometimes and it is solved just by giving enter in some line or something like that, very rare!) – Fiorella Gamarra Aguado Jan 07 '22 at 16:26

1 Answers1

0

When you are assigning IDs in layouts, make sure to use the @+id (don't forget the + sign).

<Switch android:id="@+id/theme_switch" ...
Ma3x
  • 5,761
  • 2
  • 17
  • 22