Class Cast Exception and I can't figure it out

Discussion in 'Java' started by da_rod_father, Feb 12, 2006.

  1. I am getting an exception at runtime when I try to cast a class of type
    that i created to an ArrayList:

    <% ArrayList secs = help.getList();
    if (secs != null && secs.size() >0) {
    for ( int i = 0; i < secs.size(); i++) {
    PropertyFileSectionData secName = (PropertyFileSectionData)
    secs.get(i);
    %>

    It crashes everytime I try to render this line in a jsp:

    <label><%=secName.getSectionName() %></label></td>

    I get a java.lang.ClassCastException: java.lang.String and it complains
    that java.lang.String it is not comparable to my class I created(
    PropertyFileSectionData ). Could anyone point me in the right
    direction to debug this?

    thx
     
    da_rod_father, Feb 12, 2006
    #1
    1. Advertisements

  2. da_rod_father

    Ian Mills Guest

    Please provide the source for PropertyFileSectionData, although it would
    suggest that the getSectionName method is not returning a String.
     
    Ian Mills, Feb 12, 2006
    #2
    1. Advertisements

  3. I created an interface with abstract methods.

    public interface PropertyFileSectionData
    {
    public abstract String getSectionName();
    public abstract void setSectionName(String sectionName);

    public abstract String getSectionComment();
    public abstract void setSectionComment(String sectionComment);

    public abstract PropertyFilePropAndValuePairsData
    getPropertyAndValuePairs();
    public abstract void
    setPropertyAndValuePairs(PropertyFilePropAndValuePairsData
    propAndVauePairs);

    }

    I also create an implementation class to actually do the work.

    public class PropertyFileSectionDataImpl implements
    PropertyFileSectionData
    {
    protected String _sectionName;
    protected String _sectionComment;
    protected SPPropertyFilePropAndValuePairsData _propAndValuePairs;

    public PropertyFilePropAndValuePairsData
    getPropertyAndValuePairs() {
    return _propAndValuePairs;
    }
    public void
    setPropertyAndValuePairs(PropertyFilePropAndValuePairsData
    andValuePairs) {
    _propAndValuePairs = andValuePairs;
    }
    public String getSectionComment() {
    return _sectionComment;
    }
    public void setSectionComment(String comment) {
    _sectionComment = comment;
    }
    public String getSectionName() {
    return _sectionName;
    }
    public void setSectionName(String name) {
    _sectionName = name;
    }
    }
     
    da_rod_father, Feb 12, 2006
    #3
  4. I am wondering if I am modeling this wrong? Are there any good
    examples on how to correctly implement interfaces?
     
    da_rod_father, Feb 12, 2006
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.