簡體   English   中英

@Stateless 不是通過 @Inject 在 JSF @ManagedBean 中注入的

[英]@Stateless not injected via @Inject in a JSF @ManagedBean

我正在嘗試從 URL 以 JSON 格式讀取星座運勢,然后將其保存到我的數據庫中,並且我使用的是 JSF 2.2。

我收到空指針異常:

java.lang.NullPointerException
    at com.horoskop.android.controlor.HoroscopeBean.saveTodayHoroscope(HoroscopeBean.java:40)
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 javax.el.ELUtil.invokeMethod(ELUtil.java:332)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:537)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
at com.sun.el.parser.AstValue.getValue(AstValue.java:136)
at com.sun.el.parser.AstValue.getValue(AstValue.java:204)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)

這是 ManagedBean 代碼:

@ManagedBean
@ApplicationScoped
public class HoroscopeBean {


HoroscopeParser horoscopeParser;

@Inject
HoroscopeEJB horoscopeEJB;

public void saveTodayHoroscope() throws Exception{        

    HoroscopeFeed horoscope = getTodayHoroscope();

    for(int i=0;i<horoscope.getHoroscope().size();i++){         
        Day d = new Day();
        d.setText(horoscope.getHoroscope().get(i).getTxtHrs());
        horoscopeEJB.addTodayHoroscope(d);
    }
}

public void saveLoveHoroscope() throws Exception{        

    HoroscopeFeed horoscope = getLoveHoroscope();

    for(int i=0;i<horoscope.getHoroscope().size();i++){         
        Love l = new Love();
        l.setText(horoscope.getHoroscope().get(i).getTxtHrs());
        horoscopeEJB.addLoveHoroscope(l);
    }
}

public void saveWeekHoroscope() throws Exception{        

    HoroscopeFeed horoscope = getWeekHoroscope();

    for(int i=0;i<horoscope.getHoroscope().size();i++){         
        Week w = new Week();
        w.setText(horoscope.getHoroscope().get(i).getTxtHrs());
        horoscopeEJB.addWeekHoroscope(w);
    }
}

public void saveMonthHoroscope() throws Exception{        

    HoroscopeFeed horoscope = getMonthHoroscope();

    for(int i=0;i<horoscope.getHoroscope().size();i++){         
        Month m = new Month();
        m.setText(horoscope.getHoroscope().get(i).getTxtHrs());
        horoscopeEJB.addMonthHoroscope(m);
    }
}

public HoroscopeFeed getTodayHoroscope() throws Exception{
    horoscopeParser = new HoroscopeParser();
     return horoscopeParser.getTodayHoroscope(URL);
}   

public HoroscopeFeed getWeekHoroscope() throws Exception{
    horoscopeParser = new HoroscopeParser();
    return horoscopeParser.getTodayHoroscope(URL);        
}

public HoroscopeFeed getLoveHoroscope() throws Exception{
    horoscopeParser = new HoroscopeParser();
    return  horoscopeParser.getTodayHoroscope(URL);
} 

public HoroscopeFeed getMonthHoroscope() throws Exception{
    horoscopeParser = new HoroscopeParser();
    return horoscopeParser.getTodayHoroscope(URL);
} 

EJB 代碼:

@Stateless
@LocalBean
public class HoroscopeEJB {

@PersistenceContext
EntityManager em;

public void addTodayHoroscope(Day day){

    boolean ifExist = checkDaily(day.getText());
    if(!ifExist){
        em.merge(day); 
    }

}

public void addLoveHoroscope(Love love){
     boolean ifExist = checkLove(love.getText());
    if(!ifExist){
        em.merge(love); 
    }
}

 public void addWeekHoroscope(Week week){
     boolean ifExist = checkWeek(week.getText());
    if(!ifExist){
        em.merge(week); 
    }
}

public void addMonthHoroscope(Month month){
     boolean ifExist = checkMonth(month.getText());
    if(!ifExist){
        em.merge(month); 
    }
}

private boolean checkDaily(String text){
    List<Day> results = em.createQuery("SELECT d FROM Da d WHERE d.text = :text")
                    .setParameter("text", text).getResultList();

    if(results.size() > 0){
        return true;
    } else{
        return false;
    }
}

private boolean checkLove(String text){
    List<Love> results = em.createQuery("SELECT k FROM Love l WHERE l.text = :text")
                    .setParameter("text", text).getResultList();

    if(results.size() > 0){
        return true;
    } else{
        return false;
    }
}

private boolean checkWeek(String text){
    List<Week> results = em.createQuery("SELECT w FROM Week d WHERE w.text = :text")
                    .setParameter("text", text).getResultList();

    if(results.size() > 0){
        return true;
    } else{
        return false;
    }
}

 private boolean checkMonth(String text){
    List<Month> results = em.createQuery("SELECT m FROM Month m WHERE m.text = :text")
                    .setParameter("text", text).getResultList();

    if(results.size() > 0){
        return true;
    } else{
        return false;
    }
}

}

這是星座解析器代碼,我連接到另一個 URL 並讀取 JSON,然后解析它。

public class HoroscopeParser {

public HoroscopeFeed getTodayHoroscope(String url) throws Exception{
    String content = readUrl(url);
    Gson gson = new Gson();
    return gson.fromJson(content, HoroscopeFeed.class);
}

public HoroscopeFeed getLoveHoroscope(String url) throws Exception{
    String content = readUrl(url);
    Gson gson = new Gson();
    return gson.fromJson(content, HoroscopeFeed.class);
}

public HoroscopeFeed getWeekHoroscope(String url) throws Exception{
    String content = readUrl(url);
    Gson gson = new Gson();
    return gson.fromJson(content, HoroscopeFeed.class);
}

public HoroscopeFeed getMonthHoroscope(String url) throws Exception{
    String content = readUrl(url);
    Gson gson = new Gson();
    return gson.fromJson(content, HoroscopeFeed.class);
}

private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
    URL url = new URL(urlString);
    reader = new BufferedReader(new InputStreamReader(url.openStream()));
    StringBuffer buffer = new StringBuffer();
    int read;
    char[] chars = new char[1024];
    while ((read = reader.read(chars)) != -1)
        buffer.append(chars, 0, read); 

    return buffer.toString();
} finally {
    if (reader != null)
        reader.close();
}

}

}

更新:

如果我更改為 @EJB 而不是 @Inject,則會出現此錯誤:

 javax.ejb.EJBException
at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:748)
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:698)
at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:503)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2044)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:220)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at com.sun.proxy.$Proxy233.addTodayHoroscope(Unknown Source)
at com.horoskop.android.controlor.__EJB31_Generated__HoroscopeEJB__Intf____Bean__.addTodayHoroscope(Unknown Source)
at com.horoskop.android.controlor.HoroscopeBean.saveTodayHoroscope(HoroscopeBean.java:44)
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 javax.el.ELUtil.invokeMethod(ELUtil.java:332)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:537)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
at com.sun.el.parser.AstValue.getValue(AstValue.java:136)
at com.sun.el.parser.AstValue.getValue(AstValue.java:204)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:238)

網頁.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>

好的,我發現了問題所在。

這是今天星座的 SQL 查詢中的語法拼寫錯誤。

我是這樣寫的:

"SELECT d FROM Da d WHERE d.text = :text"

它應該是這樣的:

"SELECT d FROM Day d WHERE d.text = :text"

為什么不使用@EJB 而不是@Inject?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM