By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,805 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,805 IT Pros & Developers. It's quick & easy.

Handling Class Cast Exception Correctly in Java

P: 6

I have a parent class called UserPreference. This class is composed of 2 child classes namely HighLevelUserPreference and SpecificUserPreference. Please, could you tell me whats wrong with the code below as I keep getting a class cast exception. I have used the instanceOf function to check the object type before casting. However, this doesn't seem to work. the error i am getting says: 'Exception in thread "main" java.lang.ClassCastException: HighLevelUserPreference cannot be cast to SpecificUserPreference.' i can get either a highlevel or specifc user preference, i want to be able to handle both situations. I would have thought the code below is sufficient enough but i suppose i am missing something... thanks.
Expand|Select|Wrap|Line Numbers
  1.                      if (userPreferences != null)
  2.                     {
  3.                         for (Iterator userPrefiterator = userPreferences.iterator(); userPrefiterator.hasNext();)
  4.                         {                            
  6.                             HighLevelUserPreference highUserPref = new HighLevelUserPreference();
  8.                             SpecificUserPreference specificUserPref = new SpecificUserPreference();
  11.                             if (highUserPref instanceof UserPreference) // determines type of object
  12.                             {
  13.                                 highUserPref = (HighLevelUserPreference);
  15.                                 // do something
  17.                             }
  18.                             else
  19.                                 if (specificUserPref instanceof UserPreference)
  20.                                 {
  21.                                     specificUserPref = (SpecificUserPreference);
  23.                                     // do something
  24.                                 }
  25.     }
  26.     }
Dec 20 '12 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 10K+
P: 12,422
I don't know Java but I know that anyone that can help you will need more information. Like the class definitions.
Dec 20 '12 #2

P: 6
hi rabbit, thanks for the response, class definitions of what exactly?
Expand|Select|Wrap|Line Numbers
  1. public class SpecificUserPreference extends UserPreference
Expand|Select|Wrap|Line Numbers
  1. public class HighLevelUserPreference extends UserPreference
the extend keyword is used to denote an inheritance relationship between parent class userpreference and its respective relationship with child class specific user preference and high level specific user preference. i just need to know how to cast correctly i suppose.
Dec 20 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.