“`java Java接口初始化时出现NullPointerException “`

huangapple go评论72阅读模式
英文:

NullPointerException when trying to initialize Java interfaces

问题

我遇到了一些关于Java接口的问题。基本上,我有这个ICardApplMgr,它应该被ISCPCardApplMrg继承。我的接口如下:

public interface ISCPCardApplMrg extends ICardApplMgr { }

public interface ICardApplMgr {
    public String getLossCount(String nric, boolean flag, String serialNumber);
}

对于实现类:

public class SCPCardApplMrgImpl extends BaseApplMgrImpl implements ISCPCardApplMrg {

    public SCPCardApplMrgImpl() {
        super();
    }    

    public String getLossCount(String nric, boolean flag, String serialNumber) { //logic }
}

然后,我有这个服务类,它将调用上述MgrImpl类中的函数:

@Service("cardApplication")
public class CardApplicationServiceImpl implements CardApplicationService {
    protected ISCPCardApplMrg scpCardMgr;

    public void setScpCardMgr(ISCPCardApplMrg mgr) {
        this.scpCardMgr = mgr;
    }

    public ICardApplMgr getCardAppMgr(String itemCode) {
        ICardApplMgr ret = null;
        if (Constants.SCP_ITEM_CODE.equals(itemCode)) {
            ret = scpCardMgr;
        }
        if (ret == null) {
            log.warn("Null Manager found for itemCode " + itemCode);
        }
        return ret;
    }

    public String getLossCount(String nric, boolean flag, String itemCode, String serialNumber) {
        return getCardAppMgr(itemCode).getLossCount(nric, flag, serialNumber);
    }
}

然而,当我尝试调用getLossCount()时,在该函数内部的getCardAppMgr()处,我收到了NullPointerException。错误消息如下:

Exception in executing doSCPCardLoss java.lang.NullPointerException
    at sc.appMgt.business.service.impl.CardApplicationServiceImpl.getLossCount(CardApplicationServiceImpl.java:385)
    at sc.appMgt.presentation.controller.SCPCardCardLossController.doSCPCardLoss(SCPCardCardLossController.java:187)
    // 其余错误栈信息...

有关如何解决这个问题的任何想法吗?谢谢!

英文:

I was having some problem with Java interfaces. Basically I have this ICardApplMgr which is meant to be inherited by ISCPCardApplMrg. My interfaces as such:

public interface ISCPCardApplMrg extends ICardApplMgr { }

public interface ICardApplMgr {
    public String getLossCount(String nric, boolean flag, String serialNumber);
}

For the implementation class:

public class SCPCardApplMrgImpl extends BaseApplMgrImpl implements ISCPCardApplMrg {

public SCPCardApplMrgImpl() {
	super();
}    

public String getLossCount(String nric, boolean flag, String serialNumber) { //logic }

}

Then, I got this service class which will call the function in MgrImpl class above:

@Service("cardApplication")
public class CardApplicationServiceImpl implements CardApplicationService {
    protected ISCPCardApplMrg scpCardMgr;

    public void setScpCardMgr(ISCPCardApplMrg mgr) {
	    this.scpCardMgr = mgr;
    }

    public ICardApplMgr getCardAppMgr(String itemCode) {
        ICardApplMgr ret = null;
		if (Constants.SCP_ITEM_CODE.equals(itemCode)) {
			ret = scpCardMgr;
		}
		if (ret == null) {
			log.warn("Null Manager found for itemCode " + itemCode);
		}
		return ret;
    }

    public String getLossCount(String nric, boolean flag, String itemCode, String serialNumber) {
        return getCardAppMgr(itemCode).getLossCount(nric, flag, serialNumber);
    }
}

However, when I tried to call the getLossCount(), inside that function at the getCardAppMgr(), I am getting NullPointerException. Error message as such:

Exception in executing doSCPCardLoss java.lang.NullPointerException
	at sc.appMgt.business.service.impl.CardApplicationServiceImpl.getLossCount(CardApplicationServiceImpl.java:385)
	at sc.appMgt.presentation.controller.SCPCardCardLossController.doSCPCardLoss(SCPCardCardLossController.java:187)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:286)
	at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:260)
	at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:137)
	at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:350)
	at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
	at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
	at sc.idms.common.filter.SessionFilter.doFilter(SessionFilter.java:368)
	at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
	at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3701)
	at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3667)
	at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:326)
	at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:197)
	at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
	at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:71)
	at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2443)
	at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2291)
	at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2269)
	at weblogic.servlet.internal.ServletRequestImpl.runInternal(ServletRequestImpl.java:1703)
	at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1663)
	at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:272)
	at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:352)
	at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:337)
	at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:57)
	at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
	at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:644)
	at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
	at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)

Any ideas on how to resolve this? Thanks!

答案1

得分: 0

我已成功解决了这个问题。这是由于缺少对类进行连接的注释引起的。我在以下位置添加了 @Autowired 注释:

@Autowired
protected ISCPCardApplMrg scpCardMgr;

然后在 MgrImpl 类本身中:

@Component @Qualifier("scpCardMgr")
public class SCPCardApplMrgImpl extends BaseApplMgrImpl implements ISCPCardApplMrg {
  // 代码
}
英文:

I managed to solve it. It was due to the missing annotation to wired up the classes. I added @Autowired at:

@Autowired
protected ISCPCardApplMrg scpCardMgr;

Then in the MgrImpl class itself:

@Component @Qualifier("scpCardMgr")
public class SCPCardApplMrgImpl extends BaseApplMgrImpl implements ISCPCardApplMrg {
  // codes
}

huangapple
  • 本文由 发表于 2020年8月5日 09:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63257150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定