问题
Am developing a JavaEE application using Netbeans.The Entity Manager is getting a null value in the application. The em is not getting injected. Still am getting a nullPointerException as em is getting a null value in createNamedQuery. Could anyone please let me know what am I missing.
JSP page- index.jsp:
<%@page import="managed.userBean" %>
<jsp:useBean id="bean" class="managed.userBean" scope="session" />
.....
<input type="submit" value="Submit" onclick="<jsp:scriptlet> bean.validate();</jsp:scriptlet>" />
userBean.java
package managed;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import session.UserFacade;
@ManagedBean
@SessionScoped
public class userBean implements Serializable {
private String uid, password,role,fname,lname;
private String response="" ;
@EJB
UserFacade userFacade;
....
public String validate() {
System.out.println("in validate going to user facade: " + uid + password);
response = userFacade.validateUser(uid,password);
.....
}
UserFacade.java
package session;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class UserFacade extends AbstractFacade<User> {
@PersistenceContext (unitName = "ExamSysPU")
public EntityManager em;
String role;
@Override
protected EntityManager getEntityManager() {
System.out.println("check if em is open");
return em;
}
public UserFacade() {
super(User.class);
}
public String validateUser(String uid, String password) {
...
List results = em.createNamedQuery("User.findByUid").setParameter("uid", uid).getResultList();
....
}
Persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="ExamSysPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>test</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Thanks, Shilpa
回答1:
Your problem is caused because you created UserBean
manually using <jsp:useBean>
instead of using the one created and managed by JSF for you (and thus having all the necessary dependencies auto-injected for you). Essentially, if you're manually creating and managing beans, then you should also manually be creating, managing and injecting its dependencies.
Stop reading those decade-old JSP 1.x tutorials. They are not applicable anymore these Java EE 6 days and would only confuse you as to the proper approach these days. JSP has been succeeded by Facelets (XHTML). Work through some JSF2/Facelets tutorials. Basically, your concrete problem can be solved by replacing the JSP file by a Facelets file with at least the following content:
<h:form>
<h:commandButton value="Submit" action="#{bean.validate}" />
</h:form>
See also:
- Our JSF wiki page - contains a JSF2/Facelets Hello World kickoff example and several links to sane JSF2 tutorials
来源:https://stackoverflow.com/questions/15492844/entitymanager-throwing-nullpointerexception