Sunday 15 September 2013

java - ClassCastException using Class.cast using Generics -


I am writing a general method that will validate an asset by trying class.cast on it, but I'm a classmate I'm getting the exception

... class test

  public & lt; T & gt; T received (attribute P, string prope, class and lieutenant; t & gt; clauses) throws the exception {t = null; Object Propulsion = P. Jet (prokai); If throwing new exception ("property" ("property +" + "") is zero);} {val = clazz.cast (try); // marker} hold (exception e) {new exception ("+ Property +") is the invalid value of the value type ("+ clazz +"); e);} Returning Vals;}   

... test class < /p>

  @public blank set up () {proput = new PropUtil (); Properties = new property (); properties.setProperty ("test.int.prop", "3");} @ Test Public Null Test (GET) {try (new integer (3), propUtil.get (properties, "test.int.prop", Integer.class));} (Exception e) {System.out.println (e);}}   

The code commented on MARKER is causing class class exception.

Any thoughts were highly appreciated.

properties class one hashtable Store is string objects, especially when you call setProperty . You have added string "3", the integer 3 No You are trying to effectively insert "3" in the form of a integer , so try to correctly thrash a ClassCastException

  assertEquals ("3", propUtil.get (properties, "test.int.prop", String.class));   

Or if you want to get a integer back to an integer , then just a Use to .

No comments:

Post a Comment