Call to a member function get_page_permastruct() on null

adding it to ‘init’ action hook works

function update_menu_custom(){
wp_update_nav_menu_item(2, 0, array(
    'menu-item-title' =>  __('Home'),
    'menu-item-classes' => 'home',
    'menu-item-url' => home_url( "https://wordpress.stackexchange.com/" ), 
    'menu-item-status' => 'publish'));
}

add_action('init','update_menu_custom');

But it still doesn’t works as expected, It adds too many menu items with name home, But that is a different issue