在java中使用==检查null时出现空指针异常

时间:2015-11-18 14:34:16

标签: java jenkins null jax-rs

我正在努力解决一些在jenkins上系统失败的代码,但当然在我的机器上没有问题。

public static FaultException getFaultException(String faultCode, String faultString) {
    LOGGER.debug("start getFaultException");
    Fault fault = new Fault();
    fault.setFaultCode(faultCode);
    fault.setFaultString(faultString);
    LOGGER.debug("Returning new FaultException");
    FaultException fex = new FaultException("Fault exception", fault);

    LOGGER.debug("fex is ", fex);

    try {
        if (fex == null) {
            LOGGER.debug("getFaultException fex is null");
        }
    }catch(NullPointerException nex) {
        LOGGER.debug("catched npe ", nex);
    }

    LOGGER.debug("fex class is " + fex.getClass().getCanonicalName());
    LOGGER.debug("fex built in getFaultException");
    return fex;
}

您看到的代码将显示在jenkins catched npe +空指针异常上,并将在下一行重新触发npe。所以基本上我在检查对象是否为空时有一个npe(if(fex == null)行是罪魁祸首)。 然而LOGGER.debug(" fex是",fex); line将成功转储FaultException对象。

这怎么可能? jenkinks机器运行openjdk 1.7.0_85-b01,我的机器是一台1.7.0_80的Windows机器。此外,FaultException类是一个生成的类,带有@WebFault注释,但我比较了两台机器上的结果字节码并且是相同的。 (路径和类似的东西除外)

感谢您的期待!

更新,日志文件:

            18-11-15;14:27:37;MessageContentFactory;DEBUG;start getFaultException 
            18-11-15;14:27:37;MessageContentFactory;DEBUG;Returning new FaultException 
            18-11-15;14:27:37;MessageContentFactory;DEBUG;fex is  
            [censored].service.FaultException: Fault exception
                at [censored].service.operations.common.MessageContentFactory.getFaultException(MessageContentFactory.java:98)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl$1.invoke(MockitoMethodInvocationControl.java:242)
                at org.mockito.internal.invocation.realmethod.FilteredCGLIBProxyRealMethod.invoke(FilteredCGLIBProxyRealMethod.java:27)
                at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:108)
                at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:36)
                at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationMatcher.java:34)
                at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:91)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:260)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192)
                at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
                at org.powermock.core.MockGateway.methodCall(MockGateway.java:60)
                at [censored].service.operations.common.MessageContentFactory.getFaultException(MessageContentFactory.java)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl.getFaultException(Tr069Cwmp10PortTypeImpl.java:301)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl$1.invoke(MockitoMethodInvocationControl.java:242)
                at org.mockito.internal.invocation.realmethod.FilteredCGLIBProxyRealMethod.invoke(FilteredCGLIBProxyRealMethod.java:27)
                at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:108)
                at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:36)
                at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:260)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192)
                at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
                at org.powermock.core.MockGateway.methodCall(MockGateway.java:168)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl.getFaultException(Tr069Cwmp10PortTypeImpl.java)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl.inform(Tr069Cwmp10PortTypeImpl.java:280)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl$$EnhancerByMockitoWithCGLIB$$665825f2.CGLIB$inform$10(<generated>)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl$$EnhancerByMockitoWithCGLIB$$665825f2$$FastClassByMockitoWithCGLIB$$cd84789d.invoke(<generated>)
                at org.mockito.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:216)
                at org.mockito.internal.creation.AbstractMockitoMethodProxy.invokeSuper(AbstractMockitoMethodProxy.java:10)
                at org.mockito.internal.invocation.realmethod.CGLIBProxyRealMethod.invoke(CGLIBProxyRealMethod.java:22)
                at org.mockito.internal.invocation.realmethod.FilteredCGLIBProxyRealMethod.invoke(FilteredCGLIBProxyRealMethod.java:27)
                at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:108)
                at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:36)
                at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93)
                at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
                at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:38)
                at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:51)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl$$EnhancerByMockitoWithCGLIB$$665825f2.inform(<generated>)
                at [censored].service.operations.Tr069Cwmp10PortTypeImplTest.executeTestInform(Tr069Cwmp10PortTypeImplTest.java:393)
                at [censored].service.operations.Tr069Cwmp10PortTypeImplTest.testInformInvalidParams(Tr069Cwmp10PortTypeImplTest.java:500)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
                at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
                at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
                at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
                at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
                at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
                at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
                at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
                at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
                at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
                at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
                at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
                at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
                at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
                at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
            18-11-15;14:27:37;MessageContentFactory;DEBUG;catched npe  
            java.lang.NullPointerException
                at [censored].service.operations.common.MessageContentFactory.getFaultException(MessageContentFactory.java:100)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl$1.invoke(MockitoMethodInvocationControl.java:242)
                at org.mockito.internal.invocation.realmethod.FilteredCGLIBProxyRealMethod.invoke(FilteredCGLIBProxyRealMethod.java:27)
                at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:108)
                at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:36)
                at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationMatcher.java:34)
                at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:91)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:260)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192)
                at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
                at org.powermock.core.MockGateway.methodCall(MockGateway.java:60)
                at [censored].service.operations.common.MessageContentFactory.getFaultException(MessageContentFactory.java)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl.getFaultException(Tr069Cwmp10PortTypeImpl.java:301)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl$1.invoke(MockitoMethodInvocationControl.java:242)
                at org.mockito.internal.invocation.realmethod.FilteredCGLIBProxyRealMethod.invoke(FilteredCGLIBProxyRealMethod.java:27)
                at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:108)
                at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:36)
                at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.performIntercept(MockitoMethodInvocationControl.java:260)
                at org.powermock.api.mockito.internal.invocation.MockitoMethodInvocationControl.invoke(MockitoMethodInvocationControl.java:192)
                at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:105)
                at org.powermock.core.MockGateway.methodCall(MockGateway.java:168)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl.getFaultException(Tr069Cwmp10PortTypeImpl.java)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl.inform(Tr069Cwmp10PortTypeImpl.java:280)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl$$EnhancerByMockitoWithCGLIB$$665825f2.CGLIB$inform$10(<generated>)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl$$EnhancerByMockitoWithCGLIB$$665825f2$$FastClassByMockitoWithCGLIB$$cd84789d.invoke(<generated>)
                at org.mockito.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:216)
                at org.mockito.internal.creation.AbstractMockitoMethodProxy.invokeSuper(AbstractMockitoMethodProxy.java:10)
                at org.mockito.internal.invocation.realmethod.CGLIBProxyRealMethod.invoke(CGLIBProxyRealMethod.java:22)
                at org.mockito.internal.invocation.realmethod.FilteredCGLIBProxyRealMethod.invoke(FilteredCGLIBProxyRealMethod.java:27)
                at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:108)
                at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:36)
                at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93)
                at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
                at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:38)
                at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:51)
                at [censored].service.operations.Tr069Cwmp10PortTypeImpl$$EnhancerByMockitoWithCGLIB$$665825f2.inform(<generated>)
                at [censored].service.operations.Tr069Cwmp10PortTypeImplTest.executeTestInform(Tr069Cwmp10PortTypeImplTest.java:393)
                at [censored].service.operations.Tr069Cwmp10PortTypeImplTest.testInformInvalidParams(Tr069Cwmp10PortTypeImplTest.java:500)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
                at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
                at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
                at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
                at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
                at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
                at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
                at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
                at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
                at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
                at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
                at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
                at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
                at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
                at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:606)
                at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
                at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)

更新2:FaultException.java文件:

@WebFault(name = "Fault", targetNamespace = "urn:dslforum-org:cwmp-1-0")
public class FaultException extends Exception {

private [censored].Fault fault;

public FaultException() {
    super();
}

public FaultException(String message) {
    super(message);
}

public FaultException(String message, Throwable cause) {
    super(message, cause);
}

public FaultException(String message, [censored].Fault fault) {
    super(message);
    this.fault = fault;
}

public FaultException(String message, [censored].Fault fault, Throwable cause) {
    super(message, cause);
    this.fault = fault;
}

public [censored].Fault getFaultInfo() {
    return this.fault;
}

}

1 个答案:

答案 0 :(得分:2)

你期待什么? 您在fex normaly之后使用null check

try {
    if (fex == null) {
        LOGGER.debug("getFaultException fex is null");
    }
}catch(NullPointerException nex) {
    LOGGER.debug("catched npe ", nex);
}

HERE --> LOGGER.debug("fex class is " + fex.getClass().getCanonicalName());

必须指向NPE,因为fex只有在您的第一个条件为真时才能为空。

那会更有意义:

    if (fex == null) {
        LOGGER.debug("getFaultException fex is null");
    }else{
        LOGGER.debug("fex class is " + fex.getClass().getCanonicalName());
        LOGGER.debug("fex built in getFaultException");
    }
    return fex;

顺便说一句。 try catch此处不需要NPE。它不能是thrownLOGGER null方法的第一行会抛出NPE