Hello Everybody now i working with JSF 2.0 and EJB3 and i used Primefaces to display error message or success message, i have two problem are one when i put Button sumit (JSF command button) next page Error display
/register.xhtml @26,172 value="#{userController.user.username}": Target Unreachable, 'null' returned null
my code are
User Entity
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.demoejb.entity;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author KencyWindy
*/
@Entity
@Table(name = "iuser")
@NamedQueries({
@NamedQuery(name = "Iuser.findAll", query = "SELECT i FROM Iuser i"),
@NamedQuery(name = "Iuser.findByUid", query = "SELECT i FROM Iuser i WHERE i.uid = :uid"),
@NamedQuery(name = "Iuser.findByUsername", query = "SELECT i FROM Iuser i WHERE i.username = :username"),
@NamedQuery(name = "Iuser.findByPassword", query = "SELECT i FROM Iuser i WHERE i.password = :password"),
@NamedQuery(name = "Iuser.findByPnum", query = "SELECT i FROM Iuser i WHERE i.pnum = :pnum"),
@NamedQuery(name = "Iuser.findByZipcode", query = "SELECT i FROM Iuser i WHERE i.zipcode = :zipcode"),
@NamedQuery(name = "Iuser.findByState", query = "SELECT i FROM Iuser i WHERE i.state = :state"),
@NamedQuery(name = "Iuser.findByDob", query = "SELECT i FROM Iuser i WHERE i.dob = :dob"),
@NamedQuery(name = "Iuser.findByEmail", query = "SELECT i FROM Iuser i WHERE i.email = :email"),
@NamedQuery(name = "Iuser.findByLastlogin", query = "SELECT i FROM Iuser i WHERE i.lastlogin = :lastlogin"),
@NamedQuery(name = "Iuser.findByRegdate", query = "SELECT i FROM Iuser i WHERE i.regdate = :regdate"),
@NamedQuery(name = "Iuser.findByAddress", query = "SELECT i FROM Iuser i WHERE i.address = :address"),
@NamedQuery(name = "Iuser.findByCity", query = "SELECT i FROM Iuser i WHERE i.city = :city")})
public class Iuser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "uid")
private Integer uid;
@Basic(optional = false)
@Column(name = "username")
private String username;
@Basic(optional = false)
@Column(name = "password")
private String password;
@Basic(optional = false)
@Column(name = "pnum")
private int pnum;
@Basic(optional = false)
@Column(name = "zipcode")
private int zipcode;
@Basic(optional = false)
@Column(name = "state")
private String state;
@Basic(optional = false)
@Column(name = "dob")
@Temporal(TemporalType.TIMESTAMP)
private Date dob;
@Basic(optional = false)
@Column(name = "email")
private String email;
@Column(name = "lastlogin")
@Temporal(TemporalType.TIMESTAMP)
private Date lastlogin;
@Column(name = "regdate")
@Temporal(TemporalType.TIMESTAMP)
private Date regdate;
@Basic(optional = false)
@Column(name = "address")
private String address;
@Basic(optional = false)
@Column(name = "city")
private String city;
@JoinColumn(name = "group", referencedColumnName = "g_id")
@ManyToOne(optional = false)
private Igroup igroup;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "iuser")
private List<Irent> irentList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "iuser")
private List<Ipayment> ipaymentList;
public Iuser() {
}
public Iuser(Integer uid) {
this.uid = uid;
}
public Iuser(Integer uid, String username, String password, int pnum, int zipcode, String state, Date dob, String email, String address, String city) {
this.uid = uid;
this.username = username;
this.password = password;
this.pnum = pnum;
this.zipcode = zipcode;
this.state = state;
this.dob = dob;
this.email = email;
this.address = address;
this.city = city;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPnum() {
return pnum;
}
public void setPnum(int pnum) {
this.pnum = pnum;
}
public int getZipcode() {
return zipcode;
}
public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getLastlogin() {
return lastlogin;
}
public void setLastlogin(Date lastlogin) {
this.lastlogin = lastlogin;
}
public Date getRegdate() {
return regdate;
}
public void setRegdate(Date regdate) {
this.regdate = regdate;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Igroup getIgroup() {
return igroup;
}
public void setIgroup(Igroup igroup) {
this.igroup = igroup;
}
public List<Irent> getIrentList() {
return irentList;
}
public void setIrentList(List<Irent> irentList) {
this.irentList = irentList;
}
public List<Ipayment> getIpaymentList() {
return ipaymentList;
}
public void setIpaymentList(List<Ipayment> ipaymentList) {
this.ipaymentList = ipaymentList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (uid != null ? uid.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 Iuser)) {
return false;
}
Iuser other = (Iuser) object;
if ((this.uid == null && other.uid != null) || (this.uid != null && !this.uid.equals(other.uid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.demoejb.entity.Iuser[uid=" + uid + "]";
}
}
Session Bean class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.demoejb.DAO;
import com.demoejb.entity.Igroup;
import com.demoejb.entity.Iuser;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
*
* @author KencyWindy
*/
@Stateless
@LocalBean
public class UserSessionBean {
@PersistenceContext(unitName = "DemoEJB3-ejbPU")
private EntityManager em;
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
public void persist(Object object) {
em.persist(object);
}
public Object persistEntity(Object entity) {
em.persist(entity);
return entity;
}
public Object updateEntity(Object entity){
return em.merge(entity);
}
public void removeUser(Iuser user){
user = em.find(Iuser.class, user.getUid());
}
public List<Iuser> getAllUser(){
return em.createNamedQuery("Iuser.findAll").getResultList();
}
/*Check username and email if new user enter into form
* if exist throw exception, this feature will be used in controller
*/
public Iuser checkExistUser(String email,String username){
Iuser entity = null;
try {
javax.persistence.Query query = em.createQuery("select u from iuser u where iuser.email = : email or iuser.username = :username");
query.setParameter("email", email);
query.setParameter("username", username);
entity = (Iuser) query.getResultList();
} catch (Exception e) {
}
return entity;
}
public Iuser addUser(Iuser user){
persist(user);
return user;
}
public void persist1(Object object) {
em.persist(object);
}
/*
* Get all group in database
*/
public List<Igroup> getGroup() {
return em.createNamedQuery("Igroup.findAll").getResultList();
}
}
JSF Managed Bean class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.demo.controller;
import com.demoejb.DAO.UserSessionBean;
import com.demoejb.entity.Igroup;
import com.demoejb.entity.Iuser;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
/**
*
* @author KencyWindy
*/
public class UserController {
@EJB
private UserSessionBean userSessionBean;
private Iuser user;
/** Creates a new instance of UserController */
public UserController() {
}
public Iuser getUser() {
return user;
}
public void setUser(Iuser user) {
this.user = user;
}
public String addUser(){
String toReturn = "false";
String email = user.getEmail();
String username = user.getUsername();
try {
if(userSessionBean.checkExistUser(email, username) != null){
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
"Error", "username or email id has been exist!"));
toReturn = "False";
}
else {
Igroup g = new Igroup();
Date date = new Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String getDate = sdf.format(date);
Date regDate = sdf.parse(getDate);
user.setRegdate(regDate);
user.setLastlogin(regDate);
g.setGId(1);
user.setIgroup(g);
user = userSessionBean.addUser(user);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
"Sucessful", "New user has been created!"));
}
} catch (Exception e) {
}
return toReturn;
}
/*
* Create list group for user can choose to edit
* warning !!!! this feature it can posible for administrator
*/
public javax.faces.model.SelectItem[] getGrouplist(){
SelectItem[] options = null;
List<Igroup> lGroups = userSessionBean.getGroup();
if(lGroups != null && lGroups.size() > 0){
int i = 0 ;
options = new SelectItem[lGroups.size()];
for (Igroup iGroup : lGroups){
options[i++] = new SelectItem(iGroup.getGId(), iGroup.getGName());
}
}
return options;
}
}
Face- config
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<managed-bean>
<description>Controller for enntire User module</description>
<managed-bean-name>userController</managed-bean-name>
<managed-bean-class>com.demo.controller.UserController</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
Second problem is i not give group table into combobox with h:selectOne
<h:outputLabel value="Igroup:" for="igroup" />
<h:selectOneMenu id="igroup" value="#{userController.user.igroup}" title="Igroup" required="true" requiredMessage="The Igroup field is required.">
<!-- TODO: update below reference to list of available items-->
<f:selectItems value="#{userController.grouplist}"/>
</h:selectOneMenu>
When i used error occured, i dont know why occur error? i write getGrouplist from JSFManageBean and have 1 method getAllGroup Current of table in database.