javax.faces.el.E​​valuationException:java.lang.NullPointerException

时间:2011-08-16 12:48:57

标签: java netbeans

我遇到了javax.faces.el.E​​valuationException的错误:java.lang.NullPointerException

我的控制器方法代码:

public String verify(){
String result = "failed";
int authcode = staffBean.getVerifyCodeByName(getLoginUserName()); //get verifycode from database by name
Staff temp = staffBean.find(authcode);

if ( code==authcode){
    result ="success";
    staff.setVerifystatus("Verified");
    staffBean.edit(temp);
}
return result;

}

我的StaffFacadeLocal代码:

public interface StaffFacadeLocal {

void create(Staff staff);

void edit(Staff staff);

void remove(Staff staff);

Staff find(Object id);

List<Staff> findAll();

List<Staff> findRange(int[] range);

int count();

public List findByName(String string);

public int getVerifyCodeByName(String name);

}

我的StaffFacade代码:

public class StaffFacade /*extends AbstractFacade<Staff>*/ implements StaffFacadeLocal{
@PersistenceContext(unitName = "MajorProject-ejbPU")
private EntityManager em;

protected EntityManager getEntityManager() {
    return em;
}
@Override
public void create(Staff staff) {
    em.persist(staff);
}

@Override
public void edit(Staff staff) {
    em.merge(staff);
}

@Override
public void remove(Staff staff) {
    em.remove(em.merge(staff));
}

@Override
public Staff find(Object code) {
    return em.find(Staff.class, code);
}

@Override
public List<Staff> findAll() {
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();

    cq.select(cq.from(Staff.class));
    return em.createQuery(cq).getResultList();
}

@Override
public List<Staff> findRange(int[] range) {
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(Staff.class));
    Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0]);
    q.setFirstResult(range[0]);
    return q.getResultList();
}

@Override
public int count() {
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    Root<Staff> rt = cq.from(Staff.class);
    cq.select(em.getCriteriaBuilder().count(rt));
    Query q = em.createQuery(cq);
    return ((Long) q.getSingleResult()).intValue();
}
/*public StaffFacade() {
    super(Staff.class);
}
*/
@Override
     public List<Staff> findByName(String search1) {
    Query q = em.createNamedQuery("Staff.findByName");
    q.setParameter("name", search1);
    return q.getResultList();
}



public int getVerifyCodeByName(String name) {
 Query q = em.createNamedQuery("Staff.getVerifyCodeByName");
 q.setParameter("name", name);
 try{
 int id = (int)q.getSingleResult();
 return id;
 }
 catch(NoResultException e){
     return -1;
 }
}


}

我的员工实体:

@Entity
@Table(name = "staff")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Staff.findAll", query = "SELECT s FROM Staff s"),
@NamedQuery(name = "Staff.findByStaffId", query = "SELECT s FROM Staff s WHERE s.staffId = :staffId"),
@NamedQuery(name = "Staff.findByName", query = "SELECT s FROM Staff s WHERE s.name = :name"),
@NamedQuery(name = "Staff.findByPassword", query = "SELECT s FROM Staff s WHERE s.password = :password"),
@NamedQuery(name = "Staff.findByRoles", query = "SELECT s FROM Staff s WHERE s.roles = :roles"),
@NamedQuery(name = "Staff.findByEmail", query = "SELECT s FROM Staff s WHERE s.email = :email"),
@NamedQuery(name = "Staff.getVerifyCodeByName", query = "SELECT s.verifycode FROM Staff s WHERE s.name =:name"),
@NamedQuery(name = "Staff.findByVerifystatus", query = "SELECT s FROM Staff s WHERE s.verifystatus = :verifystatus"),
@NamedQuery(name = "Staff.findByVerifycode", query = "SELECT s FROM Staff s WHERE s.verifycode = :verifycode")})
public class Staff implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
//@NotNull
@Column(name = "STAFF_ID")
private Integer staffId;
@Size(max = 30)
@Column(name = "NAME")
private String name;
@Size(max = 64)
@Column(name = "PASSWORD")
private String password;
@Size(max = 20)
@Column(name = "ROLES")
private String roles;
@Size(max = 60)
@Column(name = "EMAIL")
private String email;
@Size(max = 20)
@Column(name = "VERIFYSTATUS")
private String verifystatus;
@Basic(optional = false)
@NotNull
@Column(name = "VERIFYCODE")
private int verifycode;

public Staff() {
}

public Staff(Integer staffId) {
    this.staffId = staffId;
}

public Staff(Integer staffId, int verifycode) {
    this.staffId = staffId;
    this.verifycode = verifycode;
}

public Integer getStaffId() {
    return staffId;
}

public void setStaffId(Integer staffId) {
    this.staffId = staffId;
}

public String getName() {
    return name;
}

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

public String getPassword() {
    return password;
}

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

public String getRoles() {
    return roles;
}

public void setRoles(String roles) {
    this.roles = roles;
}

public String getEmail() {
    return email;
}

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

public String getVerifystatus() {
    return verifystatus;
}

public void setVerifystatus(String verifystatus) {
    this.verifystatus = verifystatus;
}

public int getVerifycode() {
    return verifycode;
}

public void setVerifycode(int verifycode) {
    this.verifycode = verifycode;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (staffId != null ? staffId.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Staff)) {
        return false;
    }
    Staff other = (Staff) object;
    if ((this.staffId == null && other.staffId != null) || (this.staffId != null && !this.staffId.equals(other.staffId))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "entity.Staff[ staffId=" + staffId + " ]";
}

}

我不确定我的控制器的哪个部分返回了空值。有谁可以指出我?非常感谢!

1 个答案:

答案 0 :(得分:1)

我怀疑是这段代码:

 Query q = em.createNamedQuery("Staff.getVerifyCodeByName");
 ...
 int id = (int)q.getSingleResult();

假设命名查询实际上返回Staff实体,则转换为int将失败。