sql server - java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Integer -
in project have enum :
public enum myenum { first(1), second(2); private int value; private myenum(int value) { this.value = value; } public int getvalue() { return value; } public static myenum fromvalue(int value) { (myenum e : myenum.values()) { if (e.getvalue() == value) { return e; } } return null; } and have code :
map<string, object> mymap = new hashmap<>(); // fill map data database mymap = namedparameterjdbctemplate.queryforlist(qry, paramsmap); ***if (arrays.aslist(myenum.values()) .contains(myenum.fromvalue((int) mymap.get("mykey")) )))*** { // } i exception
java.lang.classcastexception: java.lang.short cannot cast java.lang.integer** on line : **if (arrays.aslist(myenum.values()) .contains(myenum.fromvalue((int)mymap.get("mykey")) ))) mymap filled data database, knowing sql server database , mykey returned database of type tinyint in database.
can please tell me doing wrong ? thanks.
regards.
java.lang.classcastexception: java.lang.short cannot cast java.lang.integer
here mymap.get("mykey") returns short instance returned declared object object map declared map<string, object>.
it if had written :
object myvalue = mymap.get("mykey") then pass short object declared object myenum.fromvalue() has parameter int.
myenum.fromvalue((int)myvalue)); the compiler tries cast object integer.
not integer short.
cast fails.
to solve problem, should first check instance of object.
if object not instance of short, should throw exception or ignore value.
if is, should cast object short , pass short fromvalue() automatically unboxed short :
object myvalue = mymap.get("mykey"); if (!(myvalue instanceof short)){ throw new yourruntimeexception("a short expected " + myvalue.getclass()); // or ignore value } if (arrays.aslist(myenum.values()) .contains(myenum.fromvalue(((short) myvalue)) { .... }
Comments
Post a Comment