Reputation: 1
I am having problems with the site I am creating. I am new to Faces 2.0 and was only using JSP and now shifting to Faces 2.0. I have a page that adds and deletes a location Zip Code. The edit part works but the add part, I am having an error:
PropertyNotFoundException, Target Unreachable, 'null' returned null.
on following value:
value="#{zipCodeMaintennanceBean.selectedZipCode.description}"
I dont know why it does not work because the edit part works and they use the same ManagedBean.
I am using Spring 3.1.3 With Hibernate and PrimeFaces 3.5. I hope someone answers my question as I dont know how to solve this.
I suspect that whenever I click the button, a new object is being created as I am using a @RequestScoped annotation.
I tried to from instantiating the entity class on @PostConstruct Method, even instantiating the entity on the declaration and setting an empty string on description and nothing works.
It doesnt get to call save() method and seems to get stuck on the Update Model Values Phase
Below is my xhtml page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:composition template="/WEB-INF/views/template.xhtml">
<ui:define name="content">
<table border="0" width="100%">
<tr align="center">
<td><c:if test="${not empty param.login_error}">
<div class="error"
style="font-family: serif; font-size: medium; color: red;">
#{msg['label.login.invalidlogon']}<br />
#{sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
</div>
</c:if> <c:if test="">
<div class="message"
style="font-family: serif; font-size: medium; color: red;">
${sessionScope.messageVariable}</div>
</c:if> </td>
</tr>
</table>
<p:panelGrid columns="3">
<h:form id="form">
<f:facet name="header">
<p:row>
<p:column colspan="3">#{msg['usrmnt.header']}</p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<h:outputLabel for="userSearch" value="Search ZipCode/Description" />
</p:column>
<p:column>
<p:inputText styleClass="new-fld" id="userSearch"
value="#{zipCodeMaintennanceBean.searchString}">
</p:inputText>
</p:column>
<p:column>
<p:commandButton value="#{msg['usrmnt.searchbutton']}"
actionListener="#{zipCodeMaintennanceBean.search}"
update="panel,dataTable" process="@form" />
<p:commandButton value="Add"
actionListener="#{zipCodeMaintennanceBean.triggerAdd}"
update="panel,:#{p:component('displayadd')}"
oncomplete="addDialog.show()" />
</p:column>
</p:row>
<p:row>
<p:column colspan="3" styleClass="ui-widget-header">
<p:spacer height="0" />
</p:column>
</p:row>
<p:row>
<p:column colspan="3">
<p:outputPanel id="panel">
<p:dataTable id="dataTable" var="zip"
value="#{zipCodeMaintennanceBean.zipCodes}" paginator="true"
rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15"
rendered="#{zipCodeMaintennanceBean.hasSearchResult}"
selectionMode="single"
selection="#{zipCodeMaintennanceBean.selectedZipCode}"
rowKey="#{zip.zipcode}">
<p:ajax event="rowSelect"
listener="#{zipCodeMaintennanceBean.onRowSelect}"
oncomplete="editDialog.show()"
update=":#{p:component('display')}" />
<p:ajax event="rowUnselect"
listener="#{zipCodeMaintennanceBean.onRowUnselect}" />
<f:facet name="header">
Users
</f:facet>
<p:column>
<f:facet name="header">
<h:outputText value="Zip Code" />
</f:facet>
<h:outputText value="#{zip.zipcode}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Description" />
</f:facet>
<h:outputText value="#{zip.description}" />
</p:column>
</p:dataTable>
</p:outputPanel>
</p:column>
</p:row>
<p:dialog header="Add ZipCode" widgetVar="addDialog" id="addDialog"
modal="true" showEffect="clip" hideEffect="fold" dynamic="true">
<p:outputPanel autoUpdate="true">
<p:panelGrid cellpadding="4" id="displayadd"
rendered="#{zipCodeMaintennanceBean.addMode}"
styleClass="maintennancePanelGrid">
<f:facet name="header">
<p:row>
<p:column colspan="3">
<h:outputText value="Add Zip Code" />
</p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<h:outputLabel for="zipcodeAdd" value="Zip Code:" />
</p:column>
<p:column>
<p:inputText styleClass="new-fld" id="zipcodeAdd"
value="#{zipCodeMaintennanceBean.selectedZipCode.zipcode}"
label="Zip Code" required="true">
<f:validateLength for="userName" minimum="4" maximum="6"></f:validateLength>
</p:inputText>
</p:column>
<p:column>
<p:message for="zipcodeAdd"></p:message>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputLabel for="descriptionAdd" value="Description: " />
</p:column>
<p:column>
<p:inputText styleClass="new-fld" id="descriptionAdd"
required="true"
value="#{zipCodeMaintennanceBean.selectedZipCode.description}"
label="Description"></p:inputText>
</p:column>
<p:column>
<p:message for="descriptionAdd"></p:message>
</p:column>
</p:row>
<f:facet name="footer">
<p:row>
<p:column styleClass="text-align:center" colspan="3">
<p:commandButton value="Submit"
update=":#{p:component('displayadd')}"
action="#{zipCodeMaintennanceBean.add}"
oncomplete="if (!args.validationFailed) addDialog.hide()" process="@form" />
</p:column>
</p:row>
<p:row>
<p:column colspan="3">
</p:column>
</p:row>
</f:facet>
</p:panelGrid>
</p:outputPanel>
</p:dialog>
<p:dialog header="Edit ZipCode" resizable="false" id="editDialog"
widgetVar="editDialog" modal="true" showEffect="clip"
hideEffect="fold" closeOnEscape="true" dynamic="true">
<p:outputPanel autoUpdate="true">
<p:panelGrid cellpadding="4" id="display"
rendered="#{zipCodeMaintennanceBean.hasSelectedZipCode}"
styleClass="maintennancePanelGrid">
<f:facet name="header">
<p:row>
<p:column colspan="3">
<h:outputText value="Edit Zip Code" />
</p:column>
</p:row>
</f:facet>
<p:row>
<p:column>
<h:outputLabel for="zipcode" value="Zip Code:" />
</p:column>
<p:column>
<p:inputText styleClass="new-fld" id="zipcode"
value="#{zipCodeMaintennanceBean.selectedZipCode.zipcode}"
label="Zip Code" disabled="true">
<f:validateLength for="userName" minimum="4" maximum="6"></f:validateLength>
</p:inputText>
</p:column>
<p:column>
<p:message for="zipcode"></p:message>
</p:column>
</p:row>
<p:row>
<p:column>
<h:outputLabel for="descripton" value="Description: " />
</p:column>
<p:column>
<p:inputText styleClass="new-fld" id="description"
required="true"
value="#{zipCodeMaintennanceBean.selectedZipCode.description}"
label="Description"></p:inputText>
</p:column>
<p:column>
<p:message for="description"></p:message>
</p:column>
</p:row>
<f:facet name="footer">
<p:row>
<p:column styleClass="text-align:center" colspan="3">
<p:commandButton value="Submit"
update="panel,:#{p:component('display')}"
action="#{zipCodeMaintennanceBean.save}"
oncomplete="if (!args.validationFailed) editDialog.hide()" />
<p:commandButton value="Delete"
update="panel,:#{p:component('display')}"
action="#{zipCodeMaintennanceBean.delete}"
oncomplete="if (!args.validationFailed) editDialog.hide()"
process="@this">
</p:commandButton>
<p:confirmDialog global="true" showEffect="fade"
hideEffect="explode">
<p:commandButton value="Yes" type="button"
styleClass="ui-confirmdialog-yes" icon="ui-icon-check" />
<p:commandButton value="No" type="button"
styleClass="ui-confirmdialog-no" icon="ui-icon-close" />
</p:confirmDialog>
</p:column>
</p:row>
<p:row>
<p:column colspan="3">
</p:column>
</p:row>
</f:facet>
</p:panelGrid>
</p:outputPanel>
</p:dialog>
</h:form>
</p:panelGrid>
</ui:define>
</ui:composition>
</html>
Below is my Backing Bean:
package com.siteam.web.bean;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.siteam.web.invsys.dao.ZipCodeDao;
import com.siteam.web.invsys.entities.TblZipcodes;
@ManagedBean(name = "zipCodeMaintennanceBean")
@RequestScoped
@Service
public class ZipCodeMaintennanceBean {
private TblZipcodes selectedZipCode = new TblZipcodes();
private boolean hasSelectedZipCode = false;
private List<TblZipcodes> zipCodes = new ArrayList<TblZipcodes>();
private ZipCodeDao zipCodeDao;
private boolean hasSearchResult = false;
private String searchString;
private boolean addMode;
public void triggerAdd() {
init();
this.addMode = true;
}
public String add() {
try {
this.selectedZipCode.setDateupdated(Calendar.getInstance()
.getTime());
this.zipCodeDao.add(this.selectedZipCode);
this.zipCodeDao.refresh(this.selectedZipCode);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "Zip Code Saved. ",
"Saved Zip Code"));
context.getExternalContext().getFlash().setKeepMessages(true);
init();
this.addMode = false;
return "zipmnt";
} catch (DataIntegrityViolationException ex) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_WARN, "Error:",
"ZipCode Already Exists"));
ex.printStackTrace();
return "";
} catch (NullPointerException npe) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Error:", npe.getMessage()));
npe.printStackTrace();
return "";
} catch (Throwable th) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Error:", th.getCause()
.getMessage()));
th.printStackTrace();
return "";
}
}
public String save() {
try {
this.selectedZipCode.setDateupdated(Calendar.getInstance()
.getTime());
this.zipCodeDao.update(this.selectedZipCode);
this.zipCodeDao.refresh(this.selectedZipCode);
FacesContext context = FacesContext.getCurrentInstance();
init();
// RequestContext.getCurrentInstance().execute("editDialog.hide()");
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "Zip Code Saved. ",
"Saved Zip Code"));
context.getExternalContext().getFlash().setKeepMessages(true);
hasSearchResult = false;
this.hasSelectedZipCode = false;
return "zipmnt";
} catch (DataIntegrityViolationException ex) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_WARN, "Error:",
"ZipCode Already Exists"));
ex.printStackTrace();
return "";
} catch (NullPointerException npe) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Error:", npe.getMessage()));
npe.printStackTrace();
return "";
} catch (Throwable th) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Error:", th.getCause()
.getMessage()));
th.printStackTrace();
return "";
}
}
public String delete() {
try {
this.zipCodeDao.delete(this.selectedZipCode);
FacesContext context = FacesContext.getCurrentInstance();
init();
// RequestContext.getCurrentInstance().execute("editDialog.hide()");
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "Zip Code Deleted. ",
"Deleted Zip Code"));
context.getExternalContext().getFlash().setKeepMessages(true);
hasSearchResult = false;
this.hasSelectedZipCode = false;
return "zipmnt";
} catch (DataIntegrityViolationException ex) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_WARN, "Error:",
"ZipCode Already Exists"));
ex.printStackTrace();
return "";
} catch (NullPointerException npe) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Error:", npe.getMessage()));
npe.printStackTrace();
return "";
} catch (Throwable th) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Error:", th.getCause()
.getMessage()));
th.printStackTrace();
return "";
}
}
@PostConstruct
public void init() {
this.hasSelectedZipCode = false;
this.hasSearchResult = false;
this.addMode = false;
this.selectedZipCode = new TblZipcodes();
this.selectedZipCode.setDescription("");
this.zipCodes.clear();
}
public void search() {
hasSearchResult = false;
TblZipcodes zipCode = new TblZipcodes();
zipCode.setZipcode(searchString);
this.zipCodes = zipCodeDao.findZipCodes(zipCode);
if (zipCodes.size() > 0) {
hasSearchResult = true;
}
}
public TblZipcodes getSelectedZipCode() {
return selectedZipCode;
}
public void setSelectedZipCode(TblZipcodes selectedZipCode) {
this.selectedZipCode = selectedZipCode;
}
public boolean isHasSelectedZipCode() {
return hasSelectedZipCode;
}
public void setHasSelectedZipCode(boolean hasSelectedZipCode) {
this.hasSelectedZipCode = hasSelectedZipCode;
}
public List<TblZipcodes> getZipCodes() {
return zipCodes;
}
public void setZipCodes(List<TblZipcodes> zipCodes) {
this.zipCodes = zipCodes;
}
public ZipCodeDao getZipCodeDao() {
return zipCodeDao;
}
@Autowired
public void setZipCodeDao(ZipCodeDao zipCodeDao) {
this.zipCodeDao = zipCodeDao;
}
public boolean isHasSearchResult() {
return hasSearchResult;
}
public void setHasSearchResult(boolean hasSearchResult) {
this.hasSearchResult = hasSearchResult;
}
public String getSearchString() {
return searchString;
}
public void setSearchString(String searchString) {
this.searchString = searchString;
}
public void onRowSelect(SelectEvent event) {
this.selectedZipCode = (TblZipcodes) event.getObject();
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
"Selected Zip Code", "Zip Code Selected: "
+ selectedZipCode.getZipcode());
zipCodeDao.refresh(this.selectedZipCode);
FacesContext.getCurrentInstance().addMessage(null, msg);
this.hasSelectedZipCode = true;
}
public void onRowUnselect(UnselectEvent event) {
// FacesMessage msg = new FacesMessage("Car Unselected", ((Car)
// event.getObject()).getModel());
this.selectedZipCode = new TblZipcodes();
this.hasSelectedZipCode = false;
}
public boolean isAddMode() {
return addMode;
}
public void setAddMode(boolean addMode) {
this.addMode = addMode;
}
}
Below is my Zip Code Entity Code
package com.siteam.web.invsys.entities;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "TBL_ZIPCODES", schema = "INVSYS")
public class TblZipcodes implements java.io.Serializable {
private String zipcode;
private String description = "";
private Date dateupdated;
private Set<TblCustomer> tblCustomers = new HashSet<TblCustomer>(0);
public TblZipcodes() {
}
public TblZipcodes(String zipcode) {
this.zipcode = zipcode;
}
public TblZipcodes(String zipcode, String description, Date dateupdated,
Set tblCustomers) {
this.zipcode = zipcode;
this.description = description;
this.dateupdated = dateupdated;
this.tblCustomers = tblCustomers;
}
@Id
@Column(name = "ZIPCODE", unique = true, nullable = false, length = 5)
public String getZipcode() {
return this.zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
@Column(name = "DESCRIPTION", length = 50)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Temporal(TemporalType.DATE)
@Column(name = "DATEUPDATED", length = 7)
public Date getDateupdated() {
return this.dateupdated;
}
public void setDateupdated(Date dateupdated) {
this.dateupdated = dateupdated;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "tblZipcodes")
public Set<TblCustomer> getTblCustomers() {
return this.tblCustomers;
}
public void setTblCustomers(Set<TblCustomer> tblCustomers) {
this.tblCustomers = tblCustomers;
}
@Override
public boolean equals(Object newObject) {
return newObject instanceof TblZipcodes;
}
// This must return the same hashcode for every Foo object with the same key.
public int hashCode() {
return this.getClass().hashCode();
}
}
Thank you in advance.
Upvotes: 0
Views: 3020
Reputation: 4423
In your dataTable you are using the same variable for row selection.
selectionMode="single" selection="#{zipCodeMaintennanceBean.selectedZipCode}"
When no row is selected the selectedZipCode attribute will be null and you will continue to get an Exception.
You could simply use another instance of TblZipcodes for the addDialog.
PS: Some time ago someone reported a bug when giving the same name to id and widgetVar as you did in addDialog, check if this is a problem in version 3.5.
Upvotes: 1