The ClassCastException thrown to indicate that your code has attempted to 
cast an object to a subclass of which it is not an instance.  Casting only 
works when the casted object follows an is a relationship to the type you 
are trying to cast to.

When will be ClassCastException 
<http://net-informations.com/java/err/cast.htm> is thrown:


   - When you try to cast an object of Parent class to its Child class 
   type, this exception will be thrown.
   - When you try to cast an object of one class into another class type 
   that has not extended the other class or they don't have any relationship 
   between them.





On Wednesday, June 27, 2018 at 9:56:51 PM UTC+5:30, Azeem Haider wrote:
>
> I'm using jackson to read and write object into local json file.
> It successfully read and write value. But problem is when I read value 
> from file. and assign it to my object it give me an error.
>
> java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to 
> com.testing.FeedItem
>
> I know the reason of error because to read value from file jackons use 
> LinkedHashMap So, LinkedHashMap can not cast to object(FeedItem)
>
> Here is what I'm using.
>
>
> *PostFeed*
>
>
>   public class PostFeed{
>        private String name;
>        private List<FeedItem> feeditemList;
>        // other code getter setter etc
>   }
>
>
> *Reading Value from File*
>
>
> ObjectMapper objectMapper = new ObjectMapper();PostFeed postFeed = 
> objectMapper.readValue(offlinePrivateFeed, PostFeed.class);
> FeedItem feedItem = postFeed.getFeedList().get(0); // Error
>
>
> *Update Write Value to local File*
>
>
>      ObjectMapper objectMapper = new ObjectMapper();
>      objectMapper.writeValue(Utils.getOutputJsonFile("private-feed"), 
> postFeed);
>
>
> I found similar question LinkedhashMap can not cast to model Account 
> <https://stackoverflow.com/questions/28821715/java-lang-classcastexception-java-util-linkedhashmap-cannot-be-cast-to-com-test>
>  But 
> this question is not useful in my case, Almost I tried all answer but no 
> one solve my problem.
>
> Can you please let me know how can I solve this ?
>

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/865ad9ea-1b53-4c8a-a265-07f64f7442c6%40googlegroups.com.

Reply via email to