Hip Hip Array
Hip Hip Array

Reputation: 4753

Calling method from abstract class

I have the following abstract class

public abstract class ReturnAgentFromTab extends BasePage{

    @Persist("session")
    public abstract Agent getAgent();
    public abstract void setAgent(Agent agent);

    @InjectObject("spring:agentApplicationModeResolver")
    public abstract AgentApplicationModeResolver getAgentApplicationModeResolver();

    .... more @InjectObject()

    public void nextPage(IRequestCycle cycle) {

        setApplicationModeUsingAgentStatus(getAgent());

        AgentSearchNavigationManager navManager = getAgentSearchNavigationManagerFactory().getAgentSearchNavigationManager();
        FlowStage stage = getFlowStage();
        if (stage == null) {
            setApplicationModeUsingAgentStatus(getAgent());
            stage = getUserDefaultFlowStageService().getDefaultFlowStage(UserHolder.getUser(), getVisitClass().getApplicationMode());
        }

        Class nextPageClass = navManager.getNextPage(getUserDefaultFlowStageService());

        String nextPageQualifier = getUserDefaultFlowStageService().getPageQualifier(getAgent(), nextPageClass, getVisitClass().getApplicationMode());
        IPage nextPage = getPageUtils().getPage(nextPageClass, nextPageQualifier);
        if ((getFlowStage() instanceof PSDFlowStage)) {
            nextPageQualifier = getFlowStage().getValue();
        }
        nextPage = getPageUtils().getPage(nextPageClass, nextPageQualifier);
        if (navManager instanceof NameBasedAgentSearchNavigationManager && nextPageClass != SignOffStatusPage.class) {
            NameBasedAgentSearchNavigationManager nameBasedNavManager = (NameBasedAgentSearchNavigationManager) navManager;
            String nextPageName = nameBasedNavManager.getNextPageName(stage); 
            if (!nextPageName.equals(nextPageClass.getSimpleName())) {
                nextPage = getPageUtils().getPage(nextPageName, nextPageQualifier);
            }
        }

        if (isNextPageActivateAgentGeneral(nextPage)) {
            initialisePageLink(nextPageClass, nextPage);
        }
        ((WuamsBasePage) nextPage).init(getAgent().getAgentId());
        getPageUtils().navigateTo(nextPage);

    }

    private void setApplicationModeUsingAgentStatus(Agent agent) {
        getVisitClass().setApplicationMode(getHomeLinksFactory().getRegionHomeLinksService().getApplicationMode(agent));
    }

    private boolean isNextPageActivateAgentGeneral(IPage nextPage) {
        return nextPage instanceof ActiveAgentGeneralPage;
    }

    private void initialisePageLink(Class nextPageClass, IPage nextPage) {
        if (getVisitClass().getPageLink() == null) {
            getVisitClass().setPageLink(PageLinkUtil.getPageLinkMessageKeyFromPageClass(nextPageClass, 
                    getUserDefaultFlowStageService().getDefaultFlowStage(UserHolder.getUser(), getVisitClass().getApplicationMode()).getValue()));
        }
    }

}

What I want to do is call my nextPage(cycle) from another class that is abstract and extends ReturnAgentFromTab, but when I try

public abstract class DoSomethingWithAgent extends ReturnAgentFromTab {

@Persist("session")
public abstract ReturnAgentFromTab getReturnAgentFromTab();
public abstract void setReturnAgentFromTab(ReturnAgentFromTab returnAgentFromTab);
....
getReturnAgentFromTab().nextPage(cycle);

I get a null pointer exception, I know this is because I am not actually setting ReturnAgentFromTab anywhere but I do not understand how to set it using abstract classes. Can anybody help?

If ye need more code just ask

Upvotes: 2

Views: 4100

Answers (2)

PermGenError
PermGenError

Reputation: 46408

well, you cant intialize abstract class, the only way is to make some other concrete class extend your abstract class, and call the non abstract method with the concrate classes instance.

abstarct class ABS1 {
   //abstract methods
   //concreate method
   public void concMethod() {
      }
 }

public class ABS1Impl extends ABS1 {
 //implement all the abstract methods
 }
public abstract class ABS2 {
   ABS1 abs = new ABSImpl();
   abs.concMethod // 
}

Upvotes: 1

Bombe
Bombe

Reputation: 83852

The point of abstract classes is to simply not implement certain things, such as providing certain objects. The method getReturnAgentFromTab() is a perfect example: the class itself does not care where that object comes from because that is the sole responsibility of the subclass. So extend that class, write that method, and all of a sudden the base class does its thing.

Upvotes: 2

Related Questions