2

In my code query is working fine but at the time of iteration it throws this error.

My code here:

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();) {
        String plotNo = itr.next().toString();

        if(plotNo.equals("501") || plotNo.equals("520") || plotNo.equals("601"){
            System.out.println("---if---");
            //code here
        }
        else{
            System.out.println("---else---");
            Query qu1 = session.createSQLQuery("select distinct name,houseno from house_details");
            List li1 =  qu1.list();
            for (Iterator itr1 = li.iterator(); itr1.hasNext();) {
                Object[] obj = (Object[]) itr1.next();
                String houseName = (String) obj[0];
                String houseNo = (String) obj[1];
                System.out.println("---houseName--->"+houseName);
            }
    }
}catch(Exception e){
   e.printStackTrace();

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

output:

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

it shows error in Object[] obj = (Object[]) itr1.next(); line.

What's wrong in my code?

2 Answers 2

1

First, the cast: you are treating an iterator over the list li as if it were an iterator over a list of arrays, while in fact it is an iterator over a list of BigDecimals. It's li1 that you should be iterating:

for (Iterator itr1 = li1.iterator() ; itr1.hasNext() ; )
//                     ^

Another error happens early on:

List<Long> li =  qu.list();

it should be

List<BigDecimal> li =  qu.list();

because objects inside the list returned by your query are of type BigDecimal, as confirmed by the failing cast.

Sign up to request clarification or add additional context in comments.

7 Comments

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 select distinct plot_no in the first query should do the trick then.
Thank You it's working fine but some scenarios i got 501, 520, 601 values these are checking in if ** condition,so **if loop executing three time, in this scenario any possibility for execute if condition only one time
@Sri Add a boolean enteredIf variable before the loop, and set it to false. Change your condition to if (!enteredIf && (plotNo.equals("501") || plotNo.equals("520") || plotNo.equals("601"))), and set enteredIf to true inside the conditional. This way you'd process if only once, and all other times you'd be going to the else branch.
i write boolean enteredIf=false; if (!enteredIf && (plotNo.equals("501") || plotNo.equals("520") || plotNo.equals("601"))){ enteredIf=true; //my code } this process is correct? but no change in my output
|
1

As the error says: a single BigDecimal cannot be cast to an Object-Array. You have the wrong list to iterate over: li <> li1. Change for (Iterator itr1 = li.iterator(); itr1.hasNext();) { to for (Iterator itr1 = li1.iterator(); itr1.hasNext();) { and then it should work.

3 Comments

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
Add a "distinct" to your first query. Like select distinct plot_no from...
some scenarios i got 501, 520, 601 values these are checking in if ** condition,so **if loop executing three time, in this scenario any possibility for execute if condition only one time

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.