3
List queryList = executeReadAllSQLQuery(queryString);
    for (Iterator i = queryList.iterator(); i.hasNext();) {
        Object values[] = (Object[]) i.next();
        FDetails pDetails = transform(values);
        fDList.add(pDetails);
        values = null;
    }

Error I am getting at line 3 : java.math.BigDecimal cannot be cast to [Ljava.lang.Object;

My transform function :

private FDetails transform(Object[] values) {
    FDetails Details = new FDetails();
    Details.setPb((BigDecimal)values[0]);
    Details.setPm((BigDecimal)values[1]);
    Details.setEl((BigDecimal)values[1]);
    Details.setUl((BigDecimal)values[1]);
    return BalanceDetails;
}

Please help me resolve these issue.

3
  • This would be more easy, if you'd use generics instead of raw types. Commented May 3, 2013 at 9:31
  • What's the query? It returns a single BigDecimal value per row, and not an array of objects as you think it does. Commented May 3, 2013 at 9:33
  • 1
    Show us the code of executeReadAllSQLQuery(queryString); !!! Commented May 3, 2013 at 9:34

4 Answers 4

3

It's clear from error that your List queryList is in fact a list of BigDecimals. So this would be work

BigDecimal value = (BigDecimal) i.next();

but since it's not what you expect then executeReadAllSQLQuery returns wrong result

BTW for-each would look better anyway

for (Object obj : queryList) {
   ...
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for replying.But if I am using BigDecimal value = (BigDecimal) i.next(); then how could I set the attributes as in transform function() as shown.
BigDecimal value = (BigDecimal) i.next(); would return me a single BigDecimal value at a time.The rows will get changed.I want to set the values for a single row in my view
@VampireHunter: Java is very reluctant to convert between types. So you need to create an object array manually, copy the values into it and then call the method with the new array.
@Vampire Hunter You should show your queryString and executeReadAllSQLQuery method
2

How about this code:

@SuppressWarnings("rawtypes")
List<BigDecimal> queryList = executeReadAllSQLQuery(queryString);

FDetails details = new FDetails();
int i = 0;
details.setPb(queryList.get(i ++));
details.setPm(queryList.get(i ++));
...

fDList.add(pDetails);

Note: Calling fDList.add() inside of the loop is almost certainly wrong. The loop gets one value from the list but you want all five values from the list, create one instance of FDetails from those five values and add that single instance to fDList once

1 Comment

@VampireHunter: Then don't forget that other people use this site as well and they will be confused when they see questions without correct answers. Either say which of our answers was correct or post the solution.
0

The below line is returning you a java.math.BigDecimal which you are trying to cast illegaly to Object[]. It seems yourqueryList is a List<java.math.BigDecimal>.

i.next(); // is returning you a  java.math.BigDecimal

Comments

0

This works very fine!. Please try this

    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("amt", "1000");

    System.out.println("Amount is::"
            + new BigDecimal((String) map.get("amt")));

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.