'Attempt to invoke virtual method 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()' on a null object reference

I have created an Android application and I have added the "Navigation Drawer Activity" from Gallery, I have removed and renamed the menu items. After starting the application when I click to any menu item in Drawer activity I get the following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()' on a null object reference
        at androidx.navigation.ui.NavigationUI.onNavDestinationSelected(NavigationUI.java:78)
        at androidx.navigation.ui.NavigationUI$3.onNavigationItemSelected(NavigationUI.java:453)
        at com.google.android.material.navigation.NavigationView$1.onMenuItemSelected(NavigationView.java:215)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
        at com.google.android.material.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:416)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28305)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Main activity code:

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    private lateinit var appBarConfiguration: AppBarConfiguration

    private lateinit var navView: NavigationView

    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {
        setContentView(R.layout.activity_main)

        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        initFab()

        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        navView = findViewById(R.id.main_nav_view)
        val navController = findNavController(R.id.nav_host_fragment)

        navView.setNavigationItemSelectedListener(this)

        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.nav_category_list
            ),
            drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    private fun initFab() {
        val fab: FloatingActionButton = findViewById(R.id.fab)
        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }
    }

    ...
}

values-night/themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyMenuApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_200</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_200</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

values/themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyMenuApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="Theme.MyMenuApplication.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="Theme.MyMenuApplication.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="Theme.MyMenuApplication.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

Navigation:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/main_navigation"
    app:startDestination="@+id/nav_category_list">

    <fragment
        android:id="@+id/nav_category_list"
        android:name="hu.myapp.activity.main.fragment.category.CategoryFragment"
        android:label="@string/fragment_category_item_list_title"
        tools:layout="@layout/fragment_category_item_list" />

</navigation>

Menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_category_list"
            android:icon="@drawable/ic_account_balance_24"
            android:title="@string/menu_accounts"
            />

    </group>
</menu>

Debug window:

enter image description here

What can cause the NullpointerException in 'androidx.navigation.NavGraph androidx.navigation.NavDestination.getParent()'?



Solution 1:[1]

I have solved, in my fragment tag app:navGraph was missing (in layout). I have added it and that solved the nullpointer exception.

<fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:defaultNavHost="true"
...
        app:navGraph="@navigation/main_navigation"/>

Solution 2:[2]

try to downgrade the implementation about navigation, 2.3.2 has some wrong

implementation 'androidx.navigation:navigation-fragment:2.3.1'
implementation 'androidx.navigation:navigation-ui:2.3.1'

Solution 3:[3]

I got this error by having two different NavControllers in my compose hierarchy as a result of having a composable with a navController parameter: navController: NavController = rememberNavController() but not setting it to a common, single root navController.

Solution 4:[4]

I got this because I had set popUpTo my firstFragment and also handled popBackStack onBackPressed.

Solution 5:[5]

i had

app:launchSingleTop="true"

in my action

        <action
        android:id="@+id/action_confirmation_to_home_page"
        app:destination="@id/nav_graph_home"
        app:launchSingleTop="true"
        app:popUpTo="@id/home_page_fragment"
        app:popUpToInclusive="true" />

i removed it, and problem fixed

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Dániel Kis
Solution 2 ???
Solution 3 Dan Brough
Solution 4 GiridharaSPK
Solution 5 Mahdi Zareei