-1

I have done a piece of code for opening a dropdown upon selecting a value from another dropdown in jsf.While executing ,i get a problem like this:

INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 30, 2015 6:32:59 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3654 ms
Aug 30, 2015 6:33:00 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
SEVERE: Error Rendering View[/Home.xhtml]
javax.el.ELException: /Home.xhtml @57,71 value="#{authorData.car4List}": Error reading 'car4List' on type Author
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UISelectItems.getValue(UISelectItems.java:129)
    at com.sun.faces.renderkit.SelectItemsIterator.initializeItems(SelectItemsIterator.java:202)
    at com.sun.faces.renderkit.SelectItemsIterator.hasNext(SelectItemsIterator.java:135)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:761)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:843)
    at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:297)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:924)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:437)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:124)
    at com.sun.faces.lifecycle.RenderResponseP

Here is my xhtml file:

<?xml version="1.0" encoding="`enter code here`UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"    
   xmlns:h="http://java.sun.com/jsf/html">
   <h:head>

   <f:event listener="#{userData.handleEvent}" type="preRenderView" />  
   </h:head>
   <h:body>
 <f:view>
<h:form >
<table>
<tr>
<td> <h:messages showDetail="true" /> </td>
</tr>
<tr>
<td><h:outputText value="Enter name : "/></td>
<td><h:inputText value="#{userData.clientname}"/></td>
</tr>
<tr>
<td><h:outputText value="Enter Id : " /></td>
<td><h:inputText value="#{userData.id}" /></td>
</tr>
<tr>
<td></td>
<td><h:commandButton value="Insert" action="#{userData.add}"/></td>
</tr>

<tr>
<td></td>
<td>#{userData.data}</td>
</tr>
<tr>
<td><h:selectOneRadio value="#{authorData.favColor1}" id="div2">
    <f:selectItem itemValue="Red" itemLabel="Color1 - Red" />
    <f:selectItem itemValue="Green" itemLabel="Color1 - Green"/>
    <f:ajax event="click" binding="valbinding"  execute="@form" render=":div1" listener="#{authorData.setchangeval}"/>
</h:selectOneRadio></td>
</tr>
</table>
</h:form>

<h:panelGroup id="div1" style="display: #{authorData.valdisplay};">
<span>
You have selected #{authorData.message}
</span>
</h:panelGroup>
</f:view>

    <br/>
             <h:outputLabel value="Choose your Car: *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" for="firstdropdwn" />
             <h:selectOneMenu id="firstdropdwn" value="#{authorData.favoriteCar2}">

                 <f:selectItems value="#{authorData.car4List}" var="d" 
                 itemLabel="#{d.carlabel}" itemValue="#{d.carvalue}"/>

                 <f:ajax event="click" binding="valbinding1"  execute="@this firstdropdwn" render=":seconddrpdown" listener="#{authorData.setDropDwnVal}"/>  
             </h:selectOneMenu>
    <br/>  

    <h:outputLabel value="Choose your Owner: *&nbsp;&nbsp;&nbsp;" for="seconddrpdown" />      
             <h:selectOneMenu id="seconddrpdown" value="">

                <f:selectItems value="#{authorData.car3List}" var="c"
                    itemLabel="#{c.carlabel}" itemValue="#{c.carvalue}"/>

             </h:selectOneMenu>

   </h:body>
</html> 

Here is my java code(Author.java):

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ComponentSystemEvent;

@ManagedBean(name = "authorData", eager = true)
@SessionScoped
public class Author implements Serializable {
   private static final long serialVersionUID = 1L;
    String clientname;
    String id;
    String favColor1;
    public String favoriteCar2;

    private UIComponent valbinding;
    private UIComponent valbinding1;
    public List<Car> car3List;
    /*public Car[] getFavoriteCar3Value()

        {

            car3List = new Car[3];
            car3List[0] = new Car("BMW Series 4 - 316", "316");
            car3List[1] = new Car("BMW Series 4 - 318", "318");
            car3List[2] = new Car("BMW Series 4 - 320", "320");



            return car3List;

        }
   */

    public List<Car> getCar3List() {
        return car3List;
    }

    public void setCar3List(List<Car> car) {
        this.car3List = car;
    }


    public List<Car> car4List;





    public List<Car> getCar4List() {

        car4List.add(new Car("BMW","116"));
        car4List.add(new Car("AUDI","118"));
        car4List.add(new Car("MERCEDES","120"));
        return car4List;
    }



    public UIComponent getValbinding1() {
        return valbinding1;
    }






    public void setValbinding1(UIComponent valbinding1) {
        this.valbinding1 = valbinding1;
    }
    private String valdisplay="none";

    private String message;

    public String getFavoriteCar2() {
        return favoriteCar2;
    }






    public void setFavoriteCar2(String favoriteCar2) {
        this.favoriteCar2 = favoriteCar2;
    }
    private static Map<String, String> favoriteCar2Value;

        static

        {

            favoriteCar2Value = new LinkedHashMap<String, String>();
            favoriteCar2Value.put("BMW Series 1 - 116", "116"); //label, value
            favoriteCar2Value.put("BMW Series 1 - 118", "118");
            favoriteCar2Value.put("BMW Series 1 - 120", "120");

        }

       public Map<String, String> getFavoriteCar2Value()
       {
           return favoriteCar2Value;

       }






    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getValdisplay() {
        return valdisplay;
    }
    public void setValdisplay(String valdisplay) {
        this.valdisplay = valdisplay;
    }
    public UIComponent getValbinding() {
        return valbinding;
    }
    public void setValbinding(UIComponent valbinding) {
        this.valbinding = valbinding;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getClientname() {
        return clientname;
    }
    public void setClientname(String clientname) {
        this.clientname = clientname;
    }


    public String getFavColor1() {
        return favColor1;
    }
    public void setFavColor1(String favColor1) {
        this.favColor1 = favColor1;
    }
    public void setchangeval(AjaxBehaviorEvent e){
        this.setValdisplay("none");
        if(this.getFavColor1().equalsIgnoreCase("red")){
          this.setValdisplay("block");
          this.setMessage("RED");
        }else if(this.getFavColor1().equalsIgnoreCase("Green")){
            this.setValdisplay("block");
            this.setMessage("GREEN");
        }

    }

    public void setDropDwnVal(AjaxBehaviorEvent e){

        if(this.getFavoriteCar2().equalsIgnoreCase("116")){
            LinkedList<Car> ob = new LinkedList<Car>();
            ob.add(new Car("Aife","0"));
            ob.add(new Car("Anirban","1"));
            this.setCar3List(ob);

        }else {

            LinkedList<Car> ob1 = new LinkedList<Car>();
            ob1.add(new Car("Saife","0"));
            ob1.add(new Car("Abid","1"));
            this.setCar3List(ob1);

        }

    }


}

Kindly give me a solution as well as a proper code to execute that dynamically.Thanks in advance.

smoggers
  • 3,154
  • 6
  • 26
  • 38
Aife Munshi
  • 9
  • 1
  • 5

1 Answers1

0

Your Stacktrace is cut off, but I am assuming, that you are getting a NullPointerException because your field car4Listis not instantiated when you try to add a value to it. This error would not be related to JSF.

To clarify: You should initialize your list:

public List<Car> getCar4List() {
    car4List = new ArrayList<Car>();
    car4List.add(new Car("BMW","116"));
    car4List.add(new Car("AUDI","118"));
    car4List.add(new Car("MERCEDES","120"));
    return car4List;
}

or do it at field level:

private List<Car> car4List = new ArrayList<Car>();
javahippie
  • 806
  • 10
  • 25
  • I am not getting any null pointer exception. – Aife Munshi Aug 30 '15 at 17:08
  • Regarding the code you should. Please append your complete stacktrace or tell me which error you are getting. – javahippie Aug 30 '15 at 17:12
  • Sorry it was null pointer exception.i am attaching full stacktrace. – Aife Munshi Aug 30 '15 at 17:22
  • Caused by: java.lang.NullPointerException at Author.getCar4List(Author.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Metho – Aife Munshi Aug 30 '15 at 17:26
  • Updated my answer. You should also update the stack trace in your initial posting. – javahippie Aug 30 '15 at 17:29
  • public List getCar4List() { car4List = new ArrayList(); car4List.add(new Car("BMW","116")); car4List.add(new Car("AUDI","118")); car4List.add(new Car("MERCEDES","120")); return car4List; }.....the exception is not coming now but dynamically the second dropdown is not getting changed. – Aife Munshi Aug 30 '15 at 17:48
  • This is a seperate issue, you should create a new question for it, to gain more attention and keep stackoverflow clean. – javahippie Aug 30 '15 at 17:59