I’m making an app that uses a bottom navigation menu,
Each button leading to a seperate fragment e.g pressing the settings button leads to the settings fragment
The code that I’m debugging is the MainActivity code which is executed after the SplashScreen activity.
After adding the implementations and everything the code doesn’t seem to actually have any errors. But when I run the application it crashes for some reason. The logcat error goes as following:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abydos/com.example.abydos.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.bottomnavigation.BottomNavigationView.setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference
It’s basically upset about line 23 bottomNavigationView.setOnNavigationItemSelectedListener(this);
in the following MainActivity.java file:
package com.example.abydos;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity
implements BottomNavigationView.OnNavigationItemSelectedListener {
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
bottomNavigationView.setOnNavigationItemSelectedListener(this); // This is the line it's upset about
bottomNavigationView.setSelectedItemId(R.id.home);
}
HomeFragment homeFragment = new HomeFragment();
SettingsFragment settingsFragment = new SettingsFragment();
ProfileFragment profileFragment = new ProfileFragment();
MenuFragment menuFragment = new MenuFragment();
private void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fragment);
fragmentTransaction.commit();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
loadFragment(homeFragment);
return true;
case R.id.settings:
loadFragment(settingsFragment);
return true;
case R.id.profile:
loadFragment(profileFragment);
return true;
case R.id.menu:
loadFragment(menuFragment);
return true;
}
return false;
}
}
I’ve added the property in the gradle:
android.nonFinalResIds=false
to allow the switch and case to work instead of using if and else statements. I don’t think the code has anything to do with that otherwise it would output an error to me.
Adding bottomNavigationView to implement in the class also doesn’t crash the app. Note in mind the IDE isn’t showing any error what so ever its when I run the app it crashes and the log cat gives out an error.
Any help is much appreciated thanks 🙂
You declared bottomNavigationView
variable but you didn’t assign it. So it will throw NullPointerException
when you access it.
Solution
Just assign bottomNavigationView before using it.
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.yourNavigationViewId);
// other codes here
Alternatively, you can use viewBinding.
bottomNavigationView
has not been initialized so JVM throw NullPointerException. Please initialize bottomNavigationView
. Example (by using findViewById()
):
bottomNavigationView = findViewById(R.id.bottomNavigationView);
after setContentView(R.layout.activity_main)
and before bottomNavigationView.setOnNavigationItemSelectedListener(this)