2

I am using the below code to execute a command from handler class which uses command context

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

I'm explicitly calling the method getCommandContext() in same handler class which results null and so the NullPointerException thrown.

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

Now, what could i do to make this code executable?

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
Srithar
  • 41
  • 2
  • 7
  • 1
    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. – Michael Rasmussen Oct 25 '15 at 01:15
  • @MichaelRasmussen I think this should be an answer as it surely is the reason the handler class has no context set. – Sutty1000 Oct 26 '15 at 13:56

2 Answers2

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.

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

Handler method: getCommandContext(this.businessContext,this.activityTokenCallbackHandler,this.request)