web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

How to resolve System.NullReferenceException?

(0) ShareShare
ReportReport
Posted on by

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();

  • Verified answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    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.

  • Community Member Profile Picture
    on at

    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);
    }


  • Suggested answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    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.

  • BASMA Profile Picture
    932 on at

    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 :)

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 > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans