【问题标题】:Seam - @In Identity identity not injecting & throws nullpointer exceptionSeam - @In Identity 身份未注入并引发空指针异常
【发布时间】:2010-08-02 10:49:18
【问题描述】:

我正在使用 Seam 2.0,并且我的 bean 中有一个实例变量

@In Identity identity 

当我使用identity.getUserName() 时,它会抛出NullPointerException。谁能告诉我我在这里还缺少什么?


更新:我已经定义了bean实例变量如下:

@In Identity identity;

然后在我的 bean 构造函数中,我简单地添加了下面一行来欢迎用户 -

System.out.println("Welcome - " +identity.getUsername());

我在我的 Jboss 控制台上看到以下错误:

Caused by: java.lang.NullPointerException
    at com.lntebg.quality.QualityInspectionPlan.<init>(QualityInspectionPlan.java:63)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at org.jboss.seam.Component.instantiateJavaBean(Component.java:1315)
    at org.jboss.seam.Component.instantiate(Component.java:1271)
    at org.jboss.seam.Component.newInstance(Component.java:1973)
    ... 279 more

【问题讨论】:

  • 能否附上完整的堆栈跟踪信息?

标签: servlets jboss seam nullpointerexception


【解决方案1】:

一些想法:

  • 您是否在 Seam 组件中注入 Identity?
  • Seam 无法在构造函数中注入。
  • 您的 components.xml 中有这些 sn-ps 吗?

&lt;core:init security-enabled="true" /&gt;

&lt;security:identity /&gt;

【讨论】:

    【解决方案2】:

    直到构造函数被调用之后,Seam 才会注入变量,因此在构造函数中 Seam 注入的属性为空 - 因此是 NPE。

    不要在构造函数中引用 Seam 注入的属性,而是创建一个新方法,例如 init() 并使用 @Create 注释对其进行注释。在实例化对象并设置 Seam 注入属性后,将自动调用此方法。

    希望这会有所帮助,

    克里斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 2018-06-22
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      相关资源
      最近更新 更多