EntityManager为null。使用Seam和JBOSS

时间:2012-05-29 13:44:05

标签: eclipse jboss nullpointerexception seam entitymanager

我正在使用JBOSS和Seam创建一个Web应用程序,但我试图在我的一个类中使用entityManager但它是null。我把它连接到一个外部数据库并打印出类中的entityManager,它只是说null。当我尝试打开使用该类的网页时,我得到一个空指针异常,并且网页上说无法实例化Seam组件。我花了几个小时在互联网上试图弄清楚什么是错的,但我没有成功。任何帮助,将不胜感激。这是该类的代码:

package edu.uwrf.iss.flowershop.session;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.jboss.aop.util.logging.SystemOutLoggerPlugin;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import edu.uwrf.iss.flowershop.entity.FlowerStoreEmployee;



@Stateful  
@Scope(ScopeType.SESSION)
@Name("employeePort")
public class EmployeeBean implements EmployeePortal {

//These are the variables to store the employee information
String empID;
String first;
String last;
String ssn;
String phone;
String pay;
String vehicle;
String house;
String street;
String city;
String state;
String zip;

@In (create=true)
private EntityManager entityManager;
//this is the employee list
List<FlowerStoreEmployee> employeeList;

//Constructor
public EmployeeBean(){
    employeeList = new ArrayList<FlowerStoreEmployee>();
    loadEmployeeList();
}

@SuppressWarnings("unchecked")
public void loadEmployeeList(){
    employeeList = new ArrayList<FlowerStoreEmployee>();
    entityManager.isOpen();
    Query query = entityManager.createQuery("SELECT e FROM FlowerStoreVehicle e");

    employeeList.addAll((List<FlowerStoreEmployee>)query.getResultList());
}

//Getters and Setters

public List<FlowerStoreEmployee> getEmployeeList() {
    return employeeList;
}

public void setEmployeeList(List<FlowerStoreEmployee> employeeList) {
    this.employeeList = employeeList;
}

//used by the add button on the addEmp.xhtml page
public String addEmployee(){
    int id = Integer.parseInt(empID);
    int soc = Integer.parseInt(ssn);
    int paid =Integer.parseInt(pay);
    int vehID = Integer.parseInt(vehicle);
    int houseID = Integer.parseInt(house);
    Integer zCode = Integer.parseInt(zip);
    FlowerStoreEmployee n = new FlowerStoreEmployee();
    n.setNameFirst(first);
    n.setNameLast(last);
    n.setPay(pay);
    n.setPhone(phone);
    n.setSsn(ssn);
    employeeList.add(n);
    entityManager.persist(n);
    return "/employee.xhtml";
}

//used by the remove button on the remEmp.xhtml page
public String remEmployee(){
        int searchID = Integer.parseInt(empID);
        int emp = 0;
        int i = 0;
        FlowerStoreEmployee e;
        while (emp!= searchID && i<employeeList.size()){
            e = employeeList.get(i);
            emp = e.getEmployeeId();
        }
        employeeList.remove(i);
        return "/employee.xhtml";
}

//clears the variables used to temporarily store the information entered on the forms
public void clearTemps(){
    this.empID = null;
    this.first = null;
    this.last = null;
    this.ssn = null;
    this.phone=null;
    this.pay = null;
    this.vehicle = null;
    this.house = null;
    this.street=null;
    this.city=null;
    this.state=null;
    this.zip=null;
}


public String createEmp() {
    clearTemps();
    System.out.println("words");
    return "FlowerStore/addEmp.xhtml";
}


//Setters and getters

public String getEmpID() {
    return empID;
}

public void setEmpID(String empID) {
    this.empID = empID;
}

public String getFirst() {
    return first;
}

public void setFirst(String first) {
    this.first = first;
}

public String getLast() {
    return last;
}

public void setLast(String last) {
    this.last = last;
}

public String getSsn() {
    return ssn;
}

public void setSsn(String ssn) {
    this.ssn = ssn;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getPay() {
    return pay;
}

public void setPay(String pay) {
    this.pay = pay;
}

public String getVehicle() {
    return vehicle;
}

public void setVehicle(String vehicle) {
    this.vehicle = vehicle;
}

public String getHouse() {
    return house;
}

public void setHouse(String house) {
    this.house = house;
}

public String getStreet() {
    return street;
}

public void setStreet(String street) {
    this.street = street;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public String getZip() {
    return zip;
}

public void setZip(String zip) {
    this.zip = zip;
}

/* Seam/Hibernate methods */
  @Remove
  public void remove() {
  }

  @Destroy
  public void destroy() {
  }


}

如果需要任何其他信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

在构造之前不能进行注入,而是尝试将组件注入尚未创建的组件中。

尝试将loadEmployeeList设为init方法,并使用@PostConstruct注释它。 https://community.jboss.org/thread/141133