Results 1 to 2 of 2
  1. #1
    fanjavaid is offline Member
    Join Date
    Dec 2012
    Location
    Jakarta, Indonesia
    Posts
    23
    Rep Power
    0

    Default Problem Java Class cast exception

    Hello, i have problem when display the country list. I get exception :
    Java Code:
    Exception in thread "Thread-2" java.lang.ClassCastException: Cannot cast com.ikbiz.gastroscope.model.Country to java.lang.String
    	at java.lang.Class.cast(Class.java:3014)
    	at org.jdesktop.beansbinding.Binding.convertForward(Binding.java:1312)
    BUILD STOPPED (total time: 2 seconds)
    And this is my code :

    Java Code:
    public void populateCountry(PanelCompany view) {
            List<Country> countries = new ArrayList<Country>();
            Locale [] locales = Locale.getAvailableLocales();
            
            view.getCmbCountry().removeAllItems();
            for (Locale locale : locales) {
              String name = locale.getDisplayCountry();
    
              if (!"".equals(name)) {
                  countries.add(new Country(name));
              }
            }
            
            Collections.sort(countries, new CountryComparator());
            for (Country country : countries) {
              view.getCmbCountry().addItem(country);
            }
        }
        
        class CountryComparator implements Comparator<Country> {
            private Comparator comparator;
    
            CountryComparator() {
                comparator = Collator.getInstance();
            }
    
            @Override
            public int compare(Country o1, Country o2) {
                return comparator.compare(o1.name, o2.name);
            }
        }

    And this is model class for Country :
    Java Code:
    public class Country {
        public String name;
    
        public Country(String name) {
            this.name = name;
        }
        
        @Override
        public String toString() {
            return name;
        }
    }
    Please help, thank you before :)

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Problem Java Class cast exception

    There is no casting I can see in your code. So you might better check / debug what was going on in this part of code (Binding.java line 1312): org.jdesktop.beansbinding.Binding.convertForward(B inding.java:1312).

Similar Threads

  1. Need Help with Class Cast Exception
    By soxfan714 in forum New To Java
    Replies: 2
    Last Post: 11-03-2011, 04:33 PM
  2. Class Cast exception
    By henry123 in forum Advanced Java
    Replies: 4
    Last Post: 02-15-2011, 11:42 AM
  3. Cast object to custom class problem
    By trader5050 in forum New To Java
    Replies: 7
    Last Post: 11-15-2010, 10:47 AM
  4. class cast exception
    By venkatallu in forum New To Java
    Replies: 2
    Last Post: 09-02-2008, 09:50 PM
  5. Replies: 1
    Last Post: 08-02-2007, 04:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •