Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.ArrayStoreException (S.O.S)

 
Jack Danyells
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anyone has an idea as to how I can solve this please let me know:

I am running
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_07-b05)
Java HotSpot(TM) Client VM (build 1.4.2_07-b05, mixed mode)

In my method I have the following:

ProfileValue pv = new ProfileValue();
pv.setValue(rs.getString("profile_value_id"));
retVal.add(pv);


I call this method by doing the following:

Vector retVal = new Vector();
retVal = pm.setAllSubProfilesSelected(cid, topLevelIdsEqual);

Then when I try to convert the Vector into an Array it fails and throws an
java.lang.ArrayStoreException

This is the line that breaks: (all on one line)
String[] subLevelArray = (String[]) retVal.toArray(new String[retVal.size()]);
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jack,

You're putting ProfileValue objects into the Vector, and then trying to get the Vector to store them in a String[]; hence the exception. You have several choices: make the method put just the Strings into the Vector; use ProfileValue[] instead of String[] in the toArray() call -- or call toArray() with no arguments to get an Object[].
 
Jack Danyells
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I resolved this thanks for the information though, I was adding objects not strings to the Vector.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that Strings are objects, too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic