java - LinkedHashMap casts -
i have following code
// gets title, headers , data string type = (string) responsemap.get("type"); string language = (string) responsemap.get("language"); string template = (string) responsemap.get("template"); string title = (string) responsemap.get("title"); list<map> header = (list<map>) responsemap.get("headers"); object[] rows = ((list<list>) responsemap.get("data")).toarray(); list list = new arraylist(); bean line = null; linkedhashmap<string, object> hmap = null; int j, i; (j = 0; j < rows.length; j++) { hmap = (linkedhashmap<string, object>) rows[j]; line = new bean(); (i = 1; < headers.length; i++) { line.choosefield(i, headers, hmap); } list.add(line); }
in hmap = (linkedhashmap) rows[j]; line eclipse says
java.lang.classcastexception: java.util.arraylist cannot cast java.util.linkedhashmap
i don't understand error because used piece of code (with warnings worked) before in class.
public string generatereport(string[] headers, object[] data, string language, string template, string title, string type) throws jrexception, sqlexception, ioexception { int j, i; (j = 0; j < data.length; j++) { hmap = (linkedhashmap<string, object>) data[j]; line = new bean(); (i = 1; < headers.length; i++) { line.choosefield(i, headers, hmap); } list.add(line); }
why can't cast now? using windows , jdk1.6
in second case, hmap = (linkedhashmap<string, object>) data[j];
, worked because data[j]
contains linkedhashmap<string, object>
. in order verify can put thing other linkedhashmap<string, object>
in object[] data in second code snippet. same java.lang.classcastexception
.
in first code snippet, can instanceof
check before perform blind type casting. avoiding java.lang.classcastexception
.
Comments
Post a Comment