EntityManager抛出NullPointerException [英] Entitymanager throwing NullPointerException

查看:154
本文介绍了EntityManager抛出NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在使用Netbeans开发JavaEE应用程序。实体管理器在应用程序中获取了空值。 em没有注入。
仍然获得nullPointerException,因为em在createNamedQuery中获得空值。
任何人都可以让我知道我在想什么。

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页面-index.jsp:

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

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

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:

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>

谢谢,
Shilpa

Thanks, Shilpa

推荐答案

之所以引起您的问题,是因为您使用< jsp:useBean> UserBean $ c>而不是使用JSF为您创建和管理的一个(这样就为您自动注入了所有必要的依赖项)。本质上,如果要手动创建和管理bean,那么还应该手动创建,管理和注入其依赖项。

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.

停止阅读那些已有十年历史的JSP 1.x教程。它们在Java EE 6天内不再适用,并且这些天只会使您对正确的方法感到困惑。 Facelets(XHTML)继承了JSP。完成一些JSF2 / Facelets教程。基本上,可以通过用至少具有以下内容的Facelets文件替换JSP文件来解决您的具体问题:

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>



另请参见:




  • 我们的JSF Wiki页面-包含一个JSF2 / Facelets Hello World启动示例以及指向精通JSF2教程的几个链接

  • See also:

    • Our JSF wiki page - contains a JSF2/Facelets Hello World kickoff example and several links to sane JSF2 tutorials
    • 这篇关于EntityManager抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆