Wildfly 10.1 和 NullPointerException

Wildfly 10.1 and NullPointerException

请在指出这是 What is a NullPointerException, and how do I fix it? 的副本之前,请阅读 post 到最后,因为这是按照上述网站上的建议准备的!

我将我的应用程序从 Wildfly 8 移到了 Wildfly 10,令我惊讶的是我得到了 NullPointerException 而不是我认为不应该出现的地方。

AdmUserCrud.java 的一段代码 - update() 方法:

119: inst.setRoles(roles);
120: if (inst.getAllowIp()!=null && inst.getAllowIp().equals(""))
121:  inst.setAllowIp(null);

错误:

Caused by: java.lang.NullPointerException
        at com.i4u.qla.action.setting.AdmUserCrud.update(AdmUserCrud.java:120)
        at com.i4u.qla.action.setting.AdmUserCrud$Proxy$_$$_WeldSubclass.update$$super(Unknown Source)
        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.jboss.weld.interceptor.proxy.TerminalAroundInvokeInvocationContext.proceedInternal(TerminalAroundInvokeInvocationContext.java:49)
        at org.jboss.weld.interceptor.proxy.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:77)
        at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:117)
        at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorRequired.doIntercept(TransactionalInterceptorRequired.java:53)
        at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:76)
        at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:47)
        at sun.reflect.GeneratedMethodAccessor125.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.jboss.weld.interceptor.reader.SimpleInterceptorInvocation$SimpleMethodInvocation.invoke(SimpleInterceptorInvocation.java:74)
        at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeAroundInvoke(InterceptorMethodHandler.java:84)
        at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:72)
        at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:56)
        at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:79)
        at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:68)
        at com.i4u.qla.action.setting.AdmUserCrud$Proxy$_$$_WeldSubclass.update(Unknown Source)
        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 javax.el.ELUtil.invokeMethod(ELUtil.java:300)
        at javax.el.BeanELResolver.invoke(BeanELResolver.java:415)
        at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
        at com.sun.el.parser.AstValue.invoke(AstValue.java:285)
        at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
        at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
        at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
        at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
        at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
        at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
        ... 59 more

第 119 行(以及上面不可见的行)确保 inst 对象不为空。那么为什么第 120 行会引发 NullPointer 错误呢? 我真的很困惑。

可能 inst.getAllowIp() returns 在后续调用中产生不同的结果。因此,当它执行 if:

的第一部分时
(inst.getAllowIp()!=null && ...

它可能会产生一些价值,过一会儿...

.. && inst.getAllowIp().equals("") )

可能已经是 null

防止这种情况发生的一种方法(尽管它可能不是您所需要的)是只获取一次值,例如类似

的东西
inst.setRoles(roles);
String currentIp = inst.getAllowIp();
if (currentIp!=null && currentIp.equals("")) 
  ...

总的来说,我不认为它与 Wildfly 版本更改有任何直接联系。