我正在调用 userDao.findById(Integer i) 并给出 java.lang.NullPointerException 我给出了数据库中的确切 ID

I am calling userDao.findById(Integer i) and is giving java.lang.NullPointerException i am giving the exact id that is in db

这是我的代码:

public class Test_JPA {
    public static void main(String[] args) {

        UserDao uDao = new UserDaoImplement();
        User u = uDao.findById(4);
        System.out.println(u.getName());
    }

}

在我的UserDaoImplement();

@Stateless
public class UserDaoImplement implements UserDao{

private static final Log log = LogFactory.getLog(UserDaoImplement.class);
@PersistenceContext
private EntityManager entityManager;
public User findById(Integer id) {
    log.debug("getting User instance with id: " + id);
    try {
        User instance = entityManager.find(User.class, id);
        log.debug("get successful");
        return instance;
    } catch (RuntimeException re) {
        log.error("get failed", re);
        throw re;
    }
}

这是我的 persistance.xml

<persistence-unit name="WebApp" 
transaction-type="RESOURCE_LOCAL"
>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.model.Profile</class>
        <class>com.model.User</class>
<properties>
        <!-- <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />-->
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
        <property name="hibernate.connection.username" value="root"/>
        <property name="hibernate.connection.password" value=""/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <!--  <property name="hibernate.current_session_context_class" value="thread"/>
        -->
        <!-- per hibernate 4.3 -->
    <!--    <property name="hibernate.currrent_session_context_class" value="org.hibernate.context.internal.ThreadLocal‌​SessionContext"/>
    -->
</properties>
</persistence-unit>

抱歉忘记了用户实体

@实体 @Table public class 用户 {

private Integer id;
private Profile profile;
private String name;
private String email;
private String username;
private String password;

public User() {
}

public User(Profile profile, String name, String email, String username,
        String password) {
    this.profile = profile;
    this.name = name;
    this.email = email;
    this.username = username;
    this.password = password;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "profile_id")
public Profile getProfile() {
    return this.profile;
}

public void setProfile(Profile profile) {
    this.profile = profile;
}

@Column(name = "name", length = 45)
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

@Column(name = "email", length = 45)
public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

@Column(name = "username", length = 20)
public String getUsername() {
    return this.username;
}

public void setUsername(String username) {
    this.username = username;
}

@Column(name = "password", length = 20)
public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}

}

UserDaoImplement.java 中第 85 行的内容。我愿意打赌它是 entitymanager。在您的测试用例中,您似乎已经完成了 UserDao uDao = new UserDaoImplement();。您如何期望 private EntityManager entityManager; 填充到您的测试用例中的 uDao 实例中?