2 Replies Latest reply: Oct 31, 2007 10:41 AM by 807603 RSS

    Class Cast problem when attempting to add Object in TreeSet, Please Help...

    807603
      hi friends,


      I have a TreeSet Object in which i add Object of type WeatherReport but when I trying to add my Second WeatherReport Object it throwing ClassCastException please Help Me in figure out the mistake that i did...
      /*code sample of my WeatherReport.class*/
      package com;
      class WeatherReport implements Serializable
      {
      private String region;
      private String desc;
      private String temp;
      
      /*equvalent getter and setters come here*/
      }
      
      
      /*in my jsp*/
      <%@ page import="com.WeatherReport"%>
      ...
      ..
      <%
      TreeSet<com.WeatherReport> ts=new TreeSet<com.WeatherReport>();
      while(condition)
      {
      WeatherReport wp=new WeatherReport();
      /*setting data for all the Methods*/
      
      ts.add(wp);
      }
      %>
      
      Error:
      java.lang.ClassCastException: com.WeatherReport
              at java.util.TreeMap.compare(TreeMap.java:1093)
              at java.util.TreeMap.put(TreeMap.java:465)
              at java.util.TreeSet.add(TreeSet.java:210)
              at org.apache.jsp.Weather_jsp._jspService(Weather_jsp.java:138)
              at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
              at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
              at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
              at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
              at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
              at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
              at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
              at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
              at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
              at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
              at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
              at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
              at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
              at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
              at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
              at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
              at java.lang.Thread.run(Thread.java:595)
      Edited by: rajaram on Oct 31, 2007 12:56 AM
        • 1. Re: Class Cast problem when attempting to add Object in TreeSet, Please Help...
          796365
          Error:
          java.lang.ClassCastException: com.WeatherReport
          at java.util.TreeMap.compare(TreeMap.java:1093)
          It appears that com.WeatherReport objects are not comparable, and result in this ClassCastException
          when the 2nd object added to the TreeMap is compared to the 1st object, so that they can be ordered.

          Do you need to implement a comparator/comparable condition?

          My Java version appears to be different from yours, as line number 1093 is meaningless in my sources.
          Open src.zip and look at line 1093 of TreeMap.java, it is the (private) compare method code that is doing
          the compare and causing the exception to be thrown.
          • 2. Re: Class Cast problem when attempting to add Object in TreeSet, Please Help...
            807603
            hi ChuckBing,

            Thank you very much, your suggestion helps me a lot...

            I change the WeatherReport Class as follows and its working now...
            public class WeatherReport implements Serializable,Comparable {
                private String location;
                private String temp;
                private String desc;
            
                public int compareTo(Object o) {
                    
                    if(o instanceof WeatherReport)
                    {
                        WeatherReport wp=(WeatherReport)o;
                        
                        String l1=wp.getLocation();
                        
                        String l2=this.getLocation();
                        
                        return l2.compareTo(l1);
                        
                    }
                    
                    return -1;
                    
                }
            }
            Once Again Thanks a lot ...

            Edited by: rajaram on Oct 31, 2007 9:11 PM