Monday, 15 August 2011

java - Casting from Object[] to String[] gives a ClassCastException -



java - Casting from Object[] to String[] gives a ClassCastException -

the getstrings() method giving me classcastexception. can tell me how should model? thanks!

public class hw3model extends defaultlistmodel<string> { public hw3model() { super(); } public void addstring(string string) { addelement(string); } /** * array of strings in model. * @return */ public string[] getstrings() { homecoming (string[])this.toarray(); } }

the value returned toarray object array.

that is, they've declared object[], not string[], returned via object[].

this means never case string array, it's invalid.

you're going have re-create values yourself...for example

public string[] getstrings() object[] ovalues= toarray(); string[] svalues = new string[ovalues.length]; (int index = 0; index < ovalues.length; index++) { svalues[index] = ovalues[index].tostring(); } homecoming svalues; }

java swing java-7 jogl

No comments:

Post a Comment