JeffreySu/WeiXinMPSDK · error · ArgumentNullException

buttonGroupBase不可以为空!

Error message

buttonGroupBase不可以为空!

What it means

Thrown by GetMenuFromJsonResult in Senparc.Weixin.MP's menu CommonApi when the buttonGroupBase parameter is null. GetMenu calls this helper to convert the raw JSON returned by WeChat into a usable GetMenuResult, and it passes a pre-created ButtonGroup or ConditionalButtonGroup instance as the target container; if that container was not supplied (null), conversion cannot proceed, so a WeixinMenuException is raised. The fault lies with the caller passing a null button group, not with the WeChat response itself.

Solutions

  1. Pass a concrete ButtonGroupBase instance, e.g. new ButtonGroup(), or your custom subclass
  2. Fix the DI/factory that was expected to supply the button group but returned null
  3. If you call GetMenu, use an overload that constructs the default button group for you

Example fix

// before
var menu = CommonApi.GetMenuFromJsonResult(resultFull, null);
// after
var menu = CommonApi.GetMenuFromJsonResult(resultFull, new ButtonGroup());
Defensive patterns

Strategy: type-guard

Validate before calling

if (buttonGroupBase is null)
    throw new ArgumentNullException(nameof(buttonGroupBase), "Pass new ButtonGroup() or a custom ButtonGroupBase");

Type guard

static T RequireNotNull<T>(T? value) where T : class => value ?? throw new ArgumentNullException(nameof(value));
// usage: GetMenuFromJsonResult(resultFull, RequireNotNull(buttonGroupBase));

Try / catch

try
{
    var menu = CommonApi.GetMenuFromJsonResult(resultFull, buttonGroupBase);
}
catch (ArgumentNullException ex)
{
    logger.LogError(ex, "buttonGroupBase was null");
}

Prevention

When it happens

Trigger: Calling GetMenuFromJsonResult (directly or via GetMenu overloads) passing null as the buttonGroupBase argument.

Common situations: Custom ButtonGroupBase subclasses instantiated conditionally (DI returning null), copy-pasted calls that dropped the new ButtonGroup() argument.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/9cf7c1cad45c2326. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.MP/Senparc.Weixin.MP/CommonAPIs/Menu/CommonApi.Menu.Common.cs:425

                            throw new WeixinMenuException("菜单类型无法处理(二级菜单):" + subSubButton.type);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 根据微信返回的Json数据得到可用的GetMenuResult结果
        /// </summary>
        /// <param name="resultFull"></param>
        /// <param name="buttonGroupBase">ButtonGroupBase的衍生类型,可以为ButtonGroup或ConditionalButtonGroup。返回的GetMenuResult中的menu属性即为此示例。</param>
        /// <returns></returns>
        public static GetMenuResult GetMenuFromJsonResult(GetMenuResultFull resultFull, ButtonGroupBase buttonGroupBase)
        {
            GetMenuResult result = null;
            if (buttonGroupBase == null)
            {
                throw new ArgumentNullException("buttonGroupBase不可以为空!");
            }

            try
            {
                //重新整理按钮信息
                ButtonGroupBase buttonGroup = buttonGroupBase; // ?? new ButtonGroup();
                var rootButtonList = resultFull.menu.button;

                GetButtonGroup(rootButtonList, buttonGroup);//设置默认菜单
                result = new GetMenuResult(buttonGroupBase)
                {
                    menu = buttonGroup,
                    //conditionalmenu = resultFull.conditionalmenu
                };

                //设置个性化菜单列表
                if (resultFull.conditionalmenu != null)
                {

View on GitHub (pinned to be573f6f94)