2013-03-31 32 views
0

當訪問注入的服務時,如下所示test()拋出NullPointerException
如果我不注入,但使用BugService的新實例,NPE將在下一步:BugServicegetItems()拋出。
其實我覺得很難理解CDI上的JEE6教程部分,所以我想我錯過了一些非常基本的東西。感謝你的幫助。注入服務不斷拋出空指針異常

這是Java類:

package hoho.misc; 

import java.io.Serializable; 
import javax.inject.Inject; 
import javax.inject.Named; 
import hoho.service.BugService; 

@Named 
public class Printer implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Inject 
    BugService bs; 

    public static void main(String[] args) { 
     Printer lPrinter = new Printer(); 
     System.out.println(lPrinter.test()); 
    } 

    public String test(){ 
     String result = bs.getItems().toString(); 
     return result; 
    } 
} 

,並將注入的服務:

package hoho.service; 

import java.util.List; 
import javax.ejb.Stateless; 
import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext; 
import hoho.model.generated.Item; 

/** 
* Session Bean implementation class ItemService 
*/ 
@Stateless 
public class BugService { 

    /** 
    * Default constructor. 
    */ 
    public BugService() { 
    } 

    @PersistenceContext 
    EntityManager em; 

    @SuppressWarnings("unchecked") 
    public List<Item> getItems() { 
     return this.em.createQuery(
       "SELECT i FROM Item i") 
       .getResultList(); 
    } 
} 

我的JBoss部署,structure.xml

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0"> 
    <deployment> 
    <exclusions> 
     <module name="org.jboss.resteasy.resteasy-atom-provider" /> 
     <module name="org.jboss.resteasy.resteasy-cdi" /> 
     <module name="org.jboss.resteasy.resteasy-jaxrs" /> 
     <module name="org.jboss.resteasy.resteasy-jaxb-provider" /> 
     <module name="org.jboss.resteasy.resteasy-jackson-provider" /> 
     <module name="org.jboss.resteasy.resteasy-jsapi" /> 
     <module name="org.jboss.resteasy.resteasy-multipart-provider" /> 
     <module name="org.jboss.resteasy.async-http-servlet-30" /> 
     <module name="org.apache.log4j" /> 
    </exclusions> 
    </deployment> 
</jboss-deployment-structure> 

<!-- 
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0"> 
     <deployment> 
       <exclusions> 
         <module name="org.jboss.as.jaxrs"/> 
       </exclusions> 
     </deployment> 
</jboss-deployment-structure> 
--> 

這是我的beans.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd"> 

</beans> 
+0

如果你手工初始化它,因爲在這一點上,它不再是一個管理對象,但一個POJO的EJB將無法正常工作。你有* ejb-jar.xml *打包在EJB jar文件的* META-INF *目錄中嗎?你還使用了什麼App服務器? – kolossus

+0

謝謝你的評論。不,我根本沒有'ejb-jar.xml',是不是EJB 3.1的可選項? (哦****我錯過了java-ee6標籤 - 對不起!)我正在使用JBoss AS 7.1.1。 – Lester

+0

beans.xml如何?你的部署結構是什麼? – LightGuard

回答

2

除非JBOSS與我使用(glassfish)的java-ee容器完全不同,否則你不應該嘗試使用自制main方法訪問bean(因爲容器不會在場)。

一個選擇是將指定的bean綁定到某個jsf頁面,並用一個按鈕調用test()。

<h:form> 
    <h:commandButton action="#{printer.test}" value="Add"/> 
</h:form> 

我喜歡this tutorial on jsf-cdi-ejb

+0

啊,我覺得你碰到了阿克塞爾,我錯過了。 CDI是一種容器管理技術。你不能使用'new',並期望所有的東西都能爲你接線。 – LightGuard

+0

@Aksel謝謝你的回答。你的方式我得到'NPService'在'BugService'的行,代碼是'return this.em.createQuery('。 我會查看命名教程並給出一些反饋,它對我的​​幫助/沒有幫助問題 – Lester

+0

這意味着EntityManager的注入沒有發生,當您部署時,可能在服務器日誌中有某些內容? –