Revision history [back]

click to hide/show revision 1
initial version

asked 2018-11-29 03:00:01 +0800

bztom35 gravatar image bztom35

java.lang.ClassCastException: java.lang.String cannot be cast List

What is wrong with the following codes?

/* * POJO */

public class ReservoirList{

private String name; private String location;

public ReservoirList(String name, String location) {

super();
this.name = name;
this.location = location;

}

/**
 * @return the name
 */
public String getName() {
    return name;
}


/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}


/**
 * @return the location
 */
public String getLocation() {
    return location;
}


/**
 * @param location the location to set
 */
public void setLocation(String location) {
    this.location = location;
}

}


public class ReservoirData {

private List<ReservoirList> reservoirlist = new ArrayList<ReservoirList>();
private String user_id;


public ReservoirData() {

    loadData();
}


public void loadData() {
          //Here is loading the data from database.
         ...
         while(rset.next() {
          reservoirlist.add(new ReservoirListData(rset.getString(1),rset.getString(2));
          }

     }


   public List<ReservoirList> getReservoirContributors() {
   return reservoirlist;
}

public int getSizeOfList() {
 return reservoirlist.size();
 }

}


...In my

public class ReleaseComposer extends GenericForwardComposer {

    private Listbox rsListBox;

    public void doBeforeComposeChildren(Component comp) throws Exception {
    super.doBeforeComposeChildren(comp);

           //load and populate data here.

           ReservoirData  rs = new ReservoirData();
           rsList = rs.getReservoirContributors();
            comp.setAttribute("rsList", rsList); 

       }


@SuppressWarnings("unchecked")
public void doAfterCompose(Component comp) throws Exception {
    super.doAfterCompose(comp);

 }

 public void onSelect$rsListBox(Event evt) {

    //select reservoir  name:  This is where exception occurred

    error -->  ReservoirList sData =(ReservoirList)rsListBox.getSelectedItem().getValue();
           String reservoirNmae = sData.getName();
           System.out.println("You selected: " + rservoirName);

}

java.lang.ClassCastException: java.lang.String cannot be cast List

What is wrong with the following codes?


Error Messages"

|Severe: >>java.lang.ClassCastException: java.lang.String cannot be cast to releaseDAO.ReservoirList

at dataComposer.ReleaseComposer.onSelect$rsListBox(ReleaseComposer.java:216) 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.Method.invoke(Method.java:606) at org.zkoss.zk.ui.event.GenericEventListener.onEvent(GenericEventListener.java:88) at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2743) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2714) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2655) at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:534) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.sendEvent(EventProcessingThreadImpl.java:126) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:338) at org.zkoss.zk.ui.event.Events.sendEvent(Events.java:348) at org.zkoss.zk.ui.AbstractComponent$ForwardListener.onEvent(AbstractComponent.java:3181) at org.zkoss.zk.ui.AbstractComponent.onEvent(AbstractComponent.java:2743) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2714) at org.zkoss.zk.ui.AbstractComponent.service(AbstractComponent.java:2655) at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:136) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:534) at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.run(EventProcessingThreadImpl.java:461)


/* * POJO */

public class ReservoirList{

private String name; private String location;

public ReservoirList(String name, String location) {

super();
this.name = name;
this.location = location;

}

/**
 * @return the name
 */
public String getName() {
    return name;
}


/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}


/**
 * @return the location
 */
public String getLocation() {
    return location;
}


/**
 * @param location the location to set
 */
public void setLocation(String location) {
    this.location = location;
}

}


public class ReservoirData {

private List<ReservoirList> reservoirlist = new ArrayList<ReservoirList>();
private String user_id;


public ReservoirData() {

    loadData();
}


public void loadData() {
          //Here is loading the data from database.
         ...
         while(rset.next() {
          reservoirlist.add(new ReservoirListData(rset.getString(1),rset.getString(2));
          }

     }


   public List<ReservoirList> getReservoirContributors() {
   return reservoirlist;
}

public int getSizeOfList() {
 return reservoirlist.size();
 }

}


...In my

public class ReleaseComposer extends GenericForwardComposer {

    private Listbox rsListBox;

    public void doBeforeComposeChildren(Component comp) throws Exception {
    super.doBeforeComposeChildren(comp);

           //load and populate data here.

           ReservoirData  rs = new ReservoirData();
           rsList = rs.getReservoirContributors();
            comp.setAttribute("rsList", rsList); 

       }


@SuppressWarnings("unchecked")
public void doAfterCompose(Component comp) throws Exception {
    super.doAfterCompose(comp);

 }

 public void onSelect$rsListBox(Event evt) {

    //select reservoir  name:  This is where exception occurred

    error line 216 -->  ReservoirList sData =(ReservoirList)rsListBox.getSelectedItem().getValue();
           String reservoirNmae = sData.getName();
           System.out.println("You selected: " + rservoirName);

}

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More