2

我正在使用以下代码从使用命令上下文的处理程序类执行命令

CheckUserInMemberGroupCmd checkGrpCmd = (CheckUserInMemberGroupCmd) 
                            CommandFactory.createCommand(
                                    CheckUserInMemberGroupCmd.Name, 
                                    Integer.valueOf(storeId));
            checkGrpCmd.setUser(memberId);
            checkGrpCmd.setMemberGroupName(mbrName);
            checkGrpCmd.setCommandContext(getCommandContext());
            checkGrpCmd.execute();

我在同一个处理程序类中显式调用方法 getCommandContext() ,结果为 null ,因此抛出 NullPointerException 。

public CommandContext getCommandContext()
{
  String METHODNAME = "getCommandContext";
  if (this.viewCommandContext != null) {
    ECTrace.trace(0L, super.getClass().getName(), "getCommandContext", "use viewCommandContext");
    return this.viewCommandContext;
  }
  ECTrace.trace(0L, super.getClass().getName(), "getCommandContext", "use commandContext if any");
  return this.commandContext;
}

现在,我该怎么做才能使这段代码可执行?

4

2 回答 2

0
    CommandContext commandContext = null;
    BusinessContextService bcs = null;
    Boolean localBinding = Boolean.valueOf(false);

    try {
        Object localBindingValue = request.getAttribute("com.ibm.commerce.foundation.inLocalBinding");
        localBinding = Boolean.valueOf(Boolean.TRUE.equals(localBindingValue));
        bcs = BusinessContextServiceFactory.getBusinessContextService();
        ActivityData activityData = getTempActivityData(businessContext);
        ActivityToken activityToken = getActivityToken(bcs, activityData,activityTokenCallbackHandler);
        bcs.startRequest(activityToken, activityData);
        commandContext = ContextHelper.createCommandContext(activityToken);
        logger.logp(Level.INFO, CLASSNAME, methodName, "Sap1 context" + commandContext.getCurrency());
        logger.logp(Level.INFO, CLASSNAME, methodName, "Sap2 context" + commandContext);
    }catch(Exception ex){
        logger.logp(Level.FINE, CLASSNAME, methodName, "command context is null");
    }finally {
        if ((!(localBinding.booleanValue())) && (bcs != null)) {
            bcs.flushCache();
        }
    }

处理方法:getCommandContext(this.businessContext,this.activityTokenCallbackHandler,this.request)

于 2018-05-30T04:32:06.760 回答
0

I would venture a guess that the command context is null in your handler class. Command context has to be set on the handler from the command that is utilizing it. If there is a missing call to set it anywhere up the call stack you'll be missing the reference. Try debugging and chasing up the stack until you find a command that has it set.

于 2016-02-03T17:39:28.993 回答