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];
1
  • 1
    You cant cast Lang to array object Commented Apr 29, 2016 at 6:36

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you it's working fine. but always going to else loop any problem in my if loop condition
@Sri Don't compare strings with == in Java. You need to use equals instead. See How do I compare strings in Java?
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 if you want unique numbers then specify that in your SQL: select distinct plot_no from ...
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?
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();

Comments

0

try to use

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

Regards Mahesh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.