1

I'm getting values through query and next i'm trying to iterating values but is throws error

Session session = null;
   try{
     Query qu=session.createSQLQuery("select plot_no from house_details where type='duplex'");
    List<Long> li =  qu.list();
    System.out.println("---li---"+li.toString());
    for (Iterator itr = li.iterator(); itr.hasNext();) {
        Object[] obj = (Object[]) itr.next();
        String plotNo = (String) obj[0];
        if(plotNo=="501" || plotNo== "520" || plotNo== "601"){
        System.out.println("---if---");
        //code here
        }
        else{
        System.out.println("---else---");
        //code here
        }
    }
 }catch(Exception e){
       e.printStackTrace();

   }finally {
       if(session!=null){
        session.close();
     }
    }

output:

---li---[501, 0, 101, 101, 114]
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to [Ljava.lang.Object;

what is wrong in my code. Error getting this lines

Object[] obj = (Object[]) itr.next();
String plotNo = (String) obj[0];
Sri
  • 179
  • 3
  • 9
  • 19

3 Answers3

3

The call to itr.next returns a BigDecimal, not an Object[]. You are trying to cast it to an Object[]:

Object[] obj = (Object[]) itr.next();
String plotNo = (String) obj[0];

You get a ClassCastException because it is not an Object[].

Replace the two lines above with this one line:

String plotNo = itr.next().toString();
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Thank you it's working fine. but always going to **else** loop any problem in my **if** loop condition – Sri Apr 29 '16 at 06:59
  • @Sri Don't compare strings with `==` in Java. You need to use `equals` instead. See [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jesper Apr 29 '16 at 09:17
  • i'm got **101** two times it is going in **else** loop also two times,how to stop it i want only one time is enough – Sri Apr 29 '16 at 10:59
  • @Sri if you want unique numbers then specify that in your SQL: `select distinct plot_no from ...` – Jesper Apr 29 '16 at 12:15
  • Thank you it is working fine,but some scenarios i got **501, 520, 101, 101, 114,620** values,so each value checking **if** and **else** conditions and entered all times in two loops,i want once enter any loop on that it's stop the checking.Are You understand? – Sri Apr 29 '16 at 12:35
1
Object[] obj = (Object[]) itr.next();
String testName = (String) obj[0];

You expect that java understands what you want to do. Frequently it will be true, and it is described in documentation. Actually, method toString() is called when another method obtains string as arg. In your case you expect something similar on unboxing and boxing( it is only true for Object class). And method toString() won't be called.

try to use method toString() explicitly which exist in class Object and all classes inherit this. It is made special for this case.

String testName = obj[0].toString();
Alex
  • 3,923
  • 3
  • 25
  • 43
0

try to use

String testName = obj[0].toString();

Regards Mahesh

mahesh
  • 49
  • 1
  • 8