Skip to main content

Notifications

Dynamics 365 Community / Forums / Finance forum / How to resolve System....
Finance forum

How to resolve System.NullReferenceException?

Posted on by Microsoft Employee

I have code that list all main menus , then I create function to list all the menu items for every main menu:

  public static void main(Args _args)
        {
                    SysDictMenu      _menu;
                    str              formName;
                    str             menuItemName;
    
            boolean iterateMenus(SysDictMenu _cmenu)
                {
                    
                    SysMenuEnumerator menuEnum = _cmenu.getEnumerator();
                    SysDictMenu subMenu;
                    SysDictMenu parentSubMenu;
                    boolean found;

                    while (menuEnum.moveNext())
                    {
                        subMenu = menuEnum.current();
                    
                        if (subMenu.isMenu() || subMenu.isMenuReference())
                        {
                            
                            //info ("Menu "+subMenu.object().treeNodePath());
                            setPrefix(subMenu.label());
                            found = iterateMenus(subMenu);
                            if (found)
                            {
                                return found;
                            }
                        }
                        else if ((subMenu.isMenuItem()&& subMenu.menuItem().type() == MenuItemType::Display && subMenu.isVisible())
                               || (subMenu.isMenuItem()&& subMenu.menuItem().type() == MenuItemType::Action && subMenu.isVisible())
                               || (subMenu.isMenuItem()&& subMenu.menuItem().type() == MenuItemType::Output && subMenu.isVisible()))
                        {
                       
                             info(subMenu.label());


                        }
                    
                    }
                  
                    return false;
                }
              
        AxMenu mainMenu = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetMenu(menuStr(MainMenu));
        var enumerator = mainMenu.Elements.GetEnumerator();

        while (enumerator.MoveNext())
        {
            AxMenuElementMenuReference menuRef = enumerator.Current;
            TreeNode menuItemNode = TreeNode::findNode(menuRef.Name);
            _menu = SysDictMenu::newTreeNode(menuItemNode);
            iterateMenus(_menu);
        }
                
            }


An exception of type 'System.NullReferenceException' occurred in Dynamics.AX.SecurityTools.Class.FindMenuItem.netmodule but was not handled in user code

Additional information: Object reference not set to an instance of an object: in this Line : 

SysMenuEnumerator menuEnum = _cmenu.getEnumerator();

  • BASMA Profile Picture
    BASMA 920 on at
    RE: How to resolve System.NullReferenceException?

    it's a confusion with another account, now I was able to use my original account.

    ok, I will continue discussing in that thread.

    thanks :)

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to resolve System.NullReferenceException?

    It's a different exception so maybe you should post a new question.

    But seems you are trying to cast an object of type menu item into a variable of type menu reference.

    I'm quite sure I remember you already discussing that issue in another thread. Please continue discussing that problem in that thread.

    But it seems you have changed your username from Basma to Basma WED, so I can't find that old question for you from your profile.

    Why did you create another username?

    Please mark the helpful anwer(s) as verified if your question (in the title of this thread) is now resolved.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to resolve System.NullReferenceException?

    thanks Nikolaos,

    but I get another exception:

    Unable to cast object of type 'Microsoft.Dynamics.AX.Metadata.MetaModel.AxMenuElementMenuItem' to type 'Microsoft.Dynamics.AX.Metadata.MetaModel.AxMenuElementMenuReference' in this line :

    while (enumerator.MoveNext())
            {
                AxMenuElementMenuReference menuRef = enumerator.Current;
                TreeNode menuItemNode = TreeNode::findNode(menuRef.Name);
                _menu = SysDictMenu::newTreeNode(menuItemNode);
                if(_menu)
                    iterateMenus(_menu);
    }


  • Verified answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: How to resolve System.NullReferenceException?

    You need to check that _cMenu is not null before calling it's getEnumerator method.

    Or check that "_menu" is not null before calling your iterateMenus method with the _menu parameter.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,902 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,297 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans