Reputation: 994
I'm studying/training JSP and decided to add a persistence to the PrimeFaces Wizard component (http://www.primefaces.org/showcase/ui/wizard.jsf).
I got a NullPointerException when submitting the save method:
SEVERE: 'java.lang.NullPointerException' recebido ao invocar escuta de ação '#{userWizard.save}' para o componente 'j_idt56'
Mar 11, 2013 4:16:12 PM javax.faces.event.MethodExpressionActionListener processAction
SEVERE: java.lang.NullPointerException
at org.primefaces.wizard.UserRepository.adiciona(UserRepository.java:16)
at org.primefaces.wizard.UserWizard.save(UserWizard.java:50)
Mar 11, 2013 4:16:12 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: JSF1073: javax.faces.event.AbortProcessingException obtido durante o processamento de INVOKE_APPLICATION 5: UIComponent-ClientId=j_idt4:j_idt56, Message=/wizard.xhtml @128,44 actionListener="#{userWizard.save}": java.lang.NullPointerException
Mar 11, 2013 4:16:12 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
SEVERE: /wizard.xhtml @128,44 actionListener="#{userWizard.save}": java.lang.NullPointerException
javax.faces.event.AbortProcessingException: /wizard.xhtml @128,44 actionListener="#{userWizard.save}": java.lang.NullPointerException
I think this is a simple question, but since this is new for me and I've been searching, unsuccessfully, for this answer for a while, I ask for your help.
Here is the managedBean:
@ManagedBean(name = "userWizard")
@SessionScoped
public class UserWizard {
private User user = new User();
private List<User> users;
private boolean skip;
private static final Logger logger = Logger.getLogger(UserWizard.class.getName());
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public boolean isSkip() {
return skip;
}
public void setSkip(boolean skip) {
this.skip = skip;
}
public void setUsers(List<User> users) {
this.users = users;
}
public void save(ActionEvent actionEvent) {
// Persist user
EntityManager manager = this.getEntityManager();
UserRepository repository = new UserRepository(manager);
repository.adiciona(this.user);
this.user = new User();
// this.users = null;
FacesMessage msg = new FacesMessage("Successful", "Welcome:" + user.getFirstname());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public List<User> getUsers() {
if (this.users == null) {
EntityManager manager = this.getEntityManager();
UserRepository repository = new UserRepository(manager);
System.out.println("**** CHAMANDO O REPOSITORIO ****");
this.users = repository.buscaTodos();
}
return this.users;
}
public String onFlowProcess(FlowEvent event) {
logger.log(Level.INFO, "Current wizard step:{0}", event.getOldStep());
logger.log(Level.INFO, "Next step:{0}", event.getNewStep());
if (skip) {
skip = false; // reset in case user goes back
return "confirm";
} else {
return event.getNewStep();
}
}
private EntityManager getEntityManager() {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest request = (HttpServletRequest) ec.getRequest();
EntityManager manager = (EntityManager) request.getAttribute("EntityManager");
return manager;
}
}
The (partial) wizard.xhtml:
The persistence class:
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
public class UserRepository {
private EntityManager manager;
public UserRepository(EntityManager manager) {
this.manager = manager;
}
public void adiciona(User user) {
this.manager.persist(user);
}
@SuppressWarnings("unchecked")
public List<User> buscaTodos() {
Query query = this.manager.createQuery("select x from USUARIO x");
return query.getResultList();
}
}
Filter class (for the EntityManager):
@WebFilter(servletNames = { "Faces Servlet" })
public class JPAFilter implements Filter {
private EntityManagerFactory factory;
public void init(FilterConfig filterConfig) throws ServletException {
this.factory = Persistence.createEntityManagerFactory("PrimeWizard-PU");
}
public void destroy() {
this.factory.close();
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
EntityManager manager = this.factory.createEntityManager();
request.setAttribute("EntityManager", manager);
manager.getTransaction().begin();
chain.doFilter(request, response);
try {
manager.getTransaction().commit();
} catch (Exception e) {
manager.getTransaction().rollback();
} finally {
manager.close();
}
}
}
Thanks for your help.
Upvotes: 0
Views: 1019
Reputation: 3443
A NullPointerException
on this line this.manager.persist(user);
- this means your EntityManager
is null. This is not related to JSF at all. Fix the way you're obtaining an EntityManager
and you're good to go.
private EntityManager getEntityManager() {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
HttpServletRequest request = (HttpServletRequest) ec.getRequest();
EntityManager manager = (EntityManager) request.getAttribute("EntityManager");
return manager;
}
Is there anything in your project that is making the EntityManager
available in the request map? That's where I'd be looking for the problem.
Upvotes: 2