'Can't set a value in Bottom Sheet switch widget from Main Activity

This is my first try with Bottom Sheets. I have a menu button in Toolbar that shows Bottom Sheets with 3 item, one of them is Switch widget that can change theme to Dark Mode. When app starts it checkes is the Dark Mode on or off. Bottom Sheet layout

When I try to set value onCreate Method in Switch I'm getting an error:

Attempt to invoke virtual method 'void androidx.appcompat.widget.SwitchCompat.setChecked(boolean)' on a null object reference

I understand the error that Switch Widget is initialized in Bottom Sheet layout not Main and appears only when user clicks the button in Toolbar but what can I change in code?

MainActivity on Create

switchCompat = findViewById(R.id.switchMode);
    sharedPreferences = getSharedPreferences("night",0);
    Boolean booleanValue = sharedPreferences.getBoolean("night_mode", true);
    if (booleanValue){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        switchCompat.setChecked(true);
    }

    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                switchCompat.setChecked(true);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("night_mode", true);
                editor.commit();
            }
            else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                switchCompat.setChecked(false);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("night_mode", false);
                editor.commit();
            }
        }
    });

Methods after onCreate:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_lang, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.menu_more_action) {
        showBottomSheetDialog();
    }
    return super.onOptionsItemSelected(item);
}

public void showBottomSheetDialog() {

    final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
    bottomSheetDialog.setContentView(R.layout.bottom_sheet);

    LinearLayout lang = bottomSheetDialog.findViewById(R.id.languageBottom);
    LinearLayout about = bottomSheetDialog.findViewById(R.id.aboutBottom);
    LinearLayout darkMode = bottomSheetDialog.findViewById(R.id.darkModeBottom);

    lang.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showChangeLanguageDialog();
            bottomSheetDialog.dismiss();
        }
    });

    about.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openAboutAppActivity();
            bottomSheetDialog.dismiss();
        }
    });

    bottomSheetDialog.show();
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source