Class Cast Exception and I can't figure it out

D

da_rod_father

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
 
I

Ian Mills

da_rod_father said:
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.
 
D

da_rod_father

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;
}
}
 
D

da_rod_father

I am wondering if I am modeling this wrong? Are there any good
examples on how to correctly implement interfaces?
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top