• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to cast an Object into an ArrayList  RSS feed

 
Joe Tseng
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know where I'm supposed to post this so here I go...

I've inherited a class that is currently causing some headaches for me; it took me a while but I was able to isolate the offending line. But first, some background...

This class is part of a WebLogic portal application that utilizes user profiles; it's supposed to facilitate the CRUD activities against the profile properties object. The profile properties object (extends Object) stores its content in plain text, and in one of the lines of the class file the contents of this object are cast into an ArrayList object. It used to work before... But now I'm getting java.lang.ClassCastException regardless of what I try. Has anyone seen this kind of behavior before? Aren't you supposed to be able to cast a generic Object into another object of a more specific class?

Here is a snippet of the offending code:

==========

Some JSP file

PersonalSearchProfileProp p = new PersonalSearchProfileProp();
...
p.addSearch( getRequest(), 0 );

==========

PersonalSearchProfileProp.java

public PersonalSearchProfileProp() {
setDefaults();
}

private void setDefaults() {
props = new Properties();
props.setProperty( FORM_FIELDNAME_SORTORDER, FORM_FIELDNAME_DOCTITLE );
props.setProperty( FORM_FIELDNAME_RESULTCNT, "5" );
}

public void addSearch( HttpServletRequest req, int whichProp ) {
ArrayList allSearches = null;

allSearches = getProperty(req);

if ( allSearches != null )
allSearches.add( whichProp, buildProp() );

setProperty( req, allSearches );
}

private ArrayList getProperty( HttpServletRequest req ) {
ArrayList al = null;

ProfileWrapper profile = SessionHelper.getProfile( req );

if ( profile == null ) {
profile = ProfileFactory.createAnonymousProfile();
SessionHelper.putProfileInSession( req, profile );
}

try {
al = (ArrayList) profile.getProperty( PROPSET, PROPNAME );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
} catch( ProfileNotFoundException e ) {
e.printStackTrace();
} catch( RemoteException e ) {
e.printStackTrace();
}

if ( al == null )
al = new ArrayList();
return al;
}
}
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't believe you can cast an object into a collection. I've never tried, because it goes against what I know about how OOP works, and I can't see the compiler liking any attempts to do it.

You can add an object to a collection, then cast it when you extract it from the collection.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a) never cast to ArrayList, always use a List referecne at least
b) show the stack trace of your ClassCastException.
Where you are casting, print out the name of the class of the object that you are trying to case - is it the same type as that to which you are attempting to case (or a subtype)? If so, investigate the fact that the classes are loaded in two different class loaders.
 
Joe Tseng
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:
a) never cast to ArrayList, always use a List referecne at least
b) show the stack trace of your ClassCastException.
Where you are casting, print out the name of the class of the object that you are trying to case - is it the same type as that to which you are attempting to case (or a subtype)? If so, investigate the fact that the classes are loaded in two different class loaders.


a) I've already tried casting to List with the same result; I've also tried Vector. In any case, here's the content of profile.getProperty() when I have it printed to STDOUT:

Personal Searches: #Wed Oct 20 16:54:23 EDT 2004
SortField=xFileFormat
SortOrder=ASC
dDocTitle=Com
SearchName=Documents with Com

b) I don't know if this is going to be of any use, but here's a portion of the stacktrace:

java.lang.ClassCastException
at portlets.scs_custom.scs_search.Scs_searchController.Search_save(Scs_searchController.jpf:971)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.bea.wlw.netui.pageflow.FlowController.invokeActionMethod(FlowController.java:1498)
at com.bea.wlw.netui.pageflow.FlowController.getActionMethodForward(FlowController.java:1433)
at com.bea.wlw.netui.pageflow.FlowController.internalExecute(FlowController.java:764)
at com.bea.wlw.netui.pageflow.PageFlowController.internalExecute(PageFlowController.java:211)
at com.bea.wlw.netui.pageflow.FlowController.execute(FlowController.java:594)

The name of the class of the object that's being cast is Object... Here's the reference to the method I'm using to create the object:

http://e-docs.bea.com/workshop/docs81/doc/en/portal/java-class/com/bea/p13n/usermgmt/profile/ProfileWrapper.html#getProperty(java.lang.String, java.lang.String)
 
Joe Tseng
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I got it working... I can't believe it was such a simple fix...

if (profile.getProperty( PROPERTY_SET, PROPERTY_NAME_PROPERTYNAME ) instanceof ArrayList)
al = (ArrayList) profile.getProperty( PROPERTY_SET, PROPERTY_NAME_PROPERTYNAME );
else
al.add(profile.getProperty( PROPERTY_SET, PROPERTY_NAME_PROPERTYNAME ));
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!