5 Replies Latest reply on Oct 19, 2006 6:57 AM by jaikiran pai

    Help on java.lang.ClassCastException: java.lang.String

    Christopher Keith Lacno Newbie

      Hi,

      Hi all im having an error java.lang.ClassCastException: java.lang.String

      Does anyone have an idea on this error?

      My codes are:

       TblCountryFacadeLocal myCountryBean = lookupTblCountryFacade();
       List myList = myCountryBean.findAll();
       int length = myList.size();
      
       for (int i = 0; i < length; i++) {
       TblCSCSubCategory region = (TblCSCSubCategory) myList.get(i) ;
       System.out.println((i + 1)+". "+region.getStrName().toString());
       }


      And for my Entity code is:
      public List findAll() {
       return em.createQuery("select o.strCountry from TblCountry as o").getResultList();
       }


      Hope for a kind response.

      Thankz :)

        • 1. Re: Help on java.lang.ClassCastException: java.lang.String
          jaikiran pai Master

           

          select o.strCountry from TblCountry as o


          o.strCountry appears to be a column of type String. So the returned objects will be of type java.lang.String. Your code:

          TblCSCSubCategory region = (TblCSCSubCategory) myList.get(i) ;


          will have to be changed to:

          String country = (String) myList.get(i);


          • 2. Re: Help on java.lang.ClassCastException: java.lang.String
            Christopher Keith Lacno Newbie


            Hi Jaikiran,

            Have tried your advice on:

            String country = (String) myList.get(i);


            I have changed my code to:

            for (int i = 0; i < length; i++) {
             //TblCountry region = (TblCountry) myList.get(i) ;
             String country = (String) myList.get(i);
             System.out.println(country);
             }


            But it return an error. The error is:

            java.lang.ClassCastException: [Ljava.lang.Object;
            crm.ui.servlets.CountryBean.processRequest(CountryBean.java:46)
            crm.ui.servlets.CountryBean.doGet(CountryBean.java:70)
            


            Thankz in advance :)

            Keith



            • 3. Re: Help on java.lang.ClassCastException: java.lang.String
              jaikiran pai Master

               

              java.lang.ClassCastException: [Ljava.lang.Object;
              crm.ui.servlets.CountryBean.processRequest(CountryBean.java:46)


              Check line numbered 46 in the CountryBean. Looks like its casting an Object[] to something else.



              • 4. Re: Help on java.lang.ClassCastException: java.lang.String
                Christopher Keith Lacno Newbie



                Line 46 is this:

                String country = (String) myList.get(i);

                My full code is:

                protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                 throws ServletException, IOException {
                 response.setContentType("text/html;charset=UTF-8");
                 PrintWriter out = response.getWriter();
                
                 TblCountryFacadeLocal myCountryBean = lookupTblCountryFacade();
                 List myList = myCountryBean.findAllCat();
                 int length = myList.size();
                
                 out.println("<html>");
                 out.println("<head>");
                 out.println("<title>Servlet CountryBean</title>");
                 out.println("</head>");
                 out.println("<body>");
                
                 for (int i = 0; i < length; i++) {
                 //TblCountry region = (TblCountry) myList.get(i) ;
                 String country = (String) myList.get(i);
                 System.out.println(country);
                 }
                 /*for (Iterator iter = myList.iterator(); iter.hasNext();) {
                 //String cityName = ( String )new String( iter.next().toString() );
                 String cityName = ( String ) new String( iter.next().toString() );
                 out.println("<p>"+ cityName +"</p>");
                
                 }*/
                
                
                 out.println("</body>");
                 out.println("</html>");
                
                 out.close();
                 }


                Any suggestions? :)

                Thankz :)

                • 5. Re: Help on java.lang.ClassCastException: java.lang.String
                  jaikiran pai Master

                  Are you sure you changed the code at the right place. Your first post had the following code, which i asked you to change:


                  TblCountryFacadeLocal myCountryBean = lookupTblCountryFacade();
                   List myList = myCountryBean.findAll();
                   int length = myList.size();
                  
                   for (int i = 0; i < length; i++) {
                   TblCSCSubCategory region = (TblCSCSubCategory) myList.get(i) ;
                   System.out.println((i + 1)+". "+region.getStrName().toString());
                   }


                  The code that you changed has something different:

                   TblCountryFacadeLocal myCountryBean = lookupTblCountryFacade();
                   List myList = myCountryBean.findAllCat();
                   int length = myList.size();
                  
                   for (int i = 0; i < length; i++) {
                   //TblCountry region = (TblCountry) myList.get(i) ;
                   String country = (String) myList.get(i);
                   System.out.println(country);
                   }