Hi,
I found a sample
JSF W/ AJAX application on this site:
http://kafle.blogspot.com/2005/09/tutorial-using-ajax-in-jsf-application.html. The application should generate an auto-completion capability on the fields using JSF. However, it generated the following errors:
javax.servlet.ServletException: javax.servlet.jsp.JspException: javax.faces.FacesException: javax.faces.FacesException: Can't instantiate class: 'demo.SelectedCountryRecord'.. class demo.SelectedCountryRecord : javax.faces.el.EvaluationException: javax.faces.FacesException: javax.faces.FacesException: Can't instantiate class: 'demo.CountryBean'.. class demo.CountryBean : java.lang.NullPointerException
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:844)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.index_jsp._jspService(org.apache.jsp.index_jsp:92)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:147)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
root cause
javax.faces.el.EvaluationException: javax.faces.FacesException: javax.faces.FacesException: Can't instantiate class: 'demo.SelectedCountryRecord'.. class demo.SelectedCountryRecord : javax.faces.el.EvaluationException: javax.faces.FacesException: javax.faces.FacesException: Can't instantiate class: 'demo.CountryBean'.. class demo.CountryBean : java.lang.NullPointerException
com.sun.faces.el.ValueBindingImpl.getValue(ValueBindingImpl.java:206)
com.sun.faces.el.ValueBindingImpl.getValue(ValueBindingImpl.java:154)
javax.faces.component.UIOutput.getValue(UIOutput.java:147)
com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:82)
com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:191)
com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:169)
javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:712)
javax.faces.webapp.UIComponentTag.encodeEnd(UIComponentTag.java:616)
javax.faces.webapp.UIComponentTag.doEndTag(UIComponentTag.java:539)
com.sun.faces.taglib.html_basic.InputTextTag.doEndTag(InputTextTag.java:524)
org.apache.jsp.index_jsp._jspx_meth_h_inputText_0(org.apache.jsp.index_jsp:289)
org.apache.jsp.index_jsp._jspx_meth_h_form_0(org.apache.jsp.index_jsp:157)
org.apache.jsp.index_jsp._jspx_meth_f_view_0(org.apache.jsp.index_jsp:117)
org.apache.jsp.index_jsp._jspService(org.apache.jsp.index_jsp:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:147)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
These are the following codes:
for CountryBean.java:
package demo;
public class CountryBean{
String[] countries;
java.util.HashMap countryDetails = new java.util.HashMap();
public CountryBean(){
try{
//read country data from the text file
java.io.BufferedReader in=
new java.io.BufferedReader(
new java.io.InputStreamReader(
CountryBean.this.getClass().
getResourceAsStream("country.data"
)));
readData(in);
in.close();
} catch (java.io.IOException IOex) {
System.out.println("IO Error :" +IOex);
}
}
//returns country data
public java.util.HashMap getCountryDetails(){
return countryDetails;
}
//returns country list
public String[] getCountries(){
return countries;
}
//read data from the text file
//and store it in countryDetails
private void readData(java.io.BufferedReader br)
throws java.io.IOException {
int recordNum= Integer.parseInt(br.readLine());
countries = new String[recordNum];
for(int i=0; i<recordNum; i++){
String line=br.readLine();
java.util.StringTokenizer st =
new java.util.StringTokenizer(line, "\t\r");
String name= st.nextToken();
String region= st.nextToken();
String cont = st.nextToken();
String capital = st.nextToken();
String id = st.nextToken();
CountryRecord cr= new CountryRecord(name, region, cont, capital, id);
countryDetails.put(name, cr);
countries[i] = name;
}
}
}
for CountryRecord.java:
package demo;
public class CountryRecord {
String name;
String region;
String continent;
String capital;
String id;
public CountryRecord(String name,
String region,
String continent,
String capital,
String id){
this.name=name;
this.region=region;
this.continent=continent;
this.capital = capital;
this.id=id;
}
public String getName(){
return name;
}
public String getRegion(){
return region;
}
public String getContinent(){
return continent;
}
public String getCapital(){
return capital;
}
public String getId(){
return id;
}
}
for SelectedCountryRecord.java:
package demo;
public class SelectedCountryRecord{
String selectedCountry;
CountryRecord selectedCountryRecord;
java.util.HashMap countryDetails;
public SelectedCountryRecord(){
setCountryDetails();
}
public String getSelectedCountry(){
return selectedCountry;
}
public void setSelectedCountry(String selectedCountry){
this.selectedCountry = selectedCountry;
setSelectedCountryRecord();
}
public String getName(){
if(selectedCountryRecord != null)
return selectedCountryRecord.getName();
else return null;
}
public String getRegion(){
if(selectedCountryRecord != null)
return selectedCountryRecord.getRegion();
else return null;
}
public String getContinent(){
if(selectedCountryRecord != null)
return selectedCountryRecord.getContinent();
else return null;
}
public String getCapital(){
if(selectedCountryRecord != null)
return selectedCountryRecord.getCapital();
else return null;
}
public String getId(){
if(selectedCountryRecord != null) {
if (selectedCountryRecord.getId() != "") {
String id = "http://www.cia.gov/cia/publications/factbook/geos/"
+ selectedCountryRecord.getId().toLowerCase() + ".html";
return id;
}
}
return null;
}
public boolean isSelected(){
if(selectedCountryRecord != null) return true;
else return false;
}
public void setSelectedCountryRecord(){
if(selectedCountry != null) {
Object obj = countryDetails.get(selectedCountry);
if((obj != null) &&
(obj instanceof CountryRecord)) {
selectedCountryRecord = (CountryRecord) obj;
}
else selectedCountryRecord=null;
} else selectedCountryRecord = null;
}
public void setCountryDetails(){
javax.faces.context.FacesContext context =
javax.faces.context.FacesContext.getCurrentInstance();
javax.faces.application.Application app =
context.getApplication();
CountryBean cBean = (CountryBean)
app.createValueBinding("#{CountryBean}").getValue(context);
countryDetails = cBean.getCountryDetails();
}
}
for faces-config.xml:
<faces-config>
<managed-bean>
<managed-bean-name>CountryBean</managed-bean-name>
<managed-bean-class>demo.CountryBean</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>SelectedCountryRecord</managed-bean-name>
<managed-bean-class>demo.SelectedCountryRecord</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
and for index.jsp:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head>
<title>Country Details</title>
</head>
<body>
<f:view>
<h:form>
<h:outputText
value="Integrating AJAX in a simple JSF Application"
styleClass="title"/>
<br>
<br>
<h:outputText value="Enter Country : " />
<h:inputText id="autoText"
value="#{SelectedCountryRecord.selectedCountry}" />
<h:commandButton value="OK" action="success"/>
<hr>
<table>
<tr>
<td>
<h:outputText value="Country Details"
rendered="#{SelectedCountryRecord.selected}"/>
</td>
</tr>
<tr>
<td>
<h:outputText value="Name : "
rendered="#{SelectedCountryRecord.selected}"/>
<h:outputText value="#{SelectedCountryRecord.name}" />
</td>
</tr>
<tr>
<td>
<h:outputText value="Region : "
rendered="#{SelectedCountryRecord.selected}"/>
<h:outputText value="#{SelectedCountryRecord.region}" />
</td>
</tr>
<tr>
<td>
<h:outputText value="Continent : "
rendered="#{SelectedCountryRecord.selected}"/>
<h:outputText value="#{SelectedCountryRecord.continent}" />
</td>
</tr>
<tr>
<td>
<h:outputText value="Capital : "
rendered="#{SelectedCountryRecord.selected}"/>
<h:outputText value="#{SelectedCountryRecord.capital}" />
</td>
</tr>
<tr>
<td>
<h:outputText value="More Descriptions : "
rendered="#{SelectedCountryRecord.selected}"/>
<h:outputLink value="#{SelectedCountryRecord.id}">
<h:outputText value="#{SelectedCountryRecord.id}" />
</h:outputLink>
</td>
</tr>
</table>
</h:form>
</f:view>
</body>
</html>
Please help me guys in fixing this problem so that we can incorporate it on our project ....
Tnx in advance...
-virgilio