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. Advertising

  2. da_rod_father

    Ian Mills Guest

    da_rod_father wrote:
    > 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
    >

    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. Advertising

  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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. index
    Replies:
    15
    Views:
    1,747
    Oliver Wong
    May 11, 2006
  2. partybob99
    Replies:
    3
    Views:
    304
    Gary Labowitz
    Oct 17, 2003
  3. Tom Mountney
    Replies:
    6
    Views:
    575
    Mathias Panzenboeck
    Nov 21, 2006
  4. Albert Hopkins
    Replies:
    3
    Views:
    474
    Steven D'Aprano
    Dec 10, 2008
  5. Doug  Wright

    Code is leaking and I can't figure out why

    Doug Wright, Mar 11, 2006, in forum: Javascript
    Replies:
    5
    Views:
    101
    Thomas 'PointedEars' Lahn
    Mar 17, 2006
Loading...

Share This Page