ClassCastException: java.lang.Object cannot be cast to java.lang.Integer
Asked Answered
R

1

13

The root of my problem is that I have a method that handles JDBC queries and releases all connections after the query. A "ResultSet" is passed back to the calling method.

I have found that I can't simply pass the ResultSet back to the calling method, because with the ResultSet closed, then any attempts to use it get an Already Closed error.

So before I close the resources, I loop through the ResultSet and store it in an ArrayList.

Because the method handles any query, I don't know what kind of types are being returned. Therefore the ArrayList stores generic s.

This works except for one field in one table .. in one database, that is an Integer[] field.

What I get out of there is a JDBC4Array object, and I have a heck of a time getting that to an Integer[] for storing in the ArrayList. I do need it to be an Integer[].

This is what I have right now... It's after lots of frustrated banjaxxing it.

While looping through the ResultSet, before the connection is closed, I do this:

            // For every row in the ResultSet
            while (rs.next()) {
                // Initialize a ITILRow for this ResultSet row
                ITILRow row = new ITILRow();

                // For each column in this row, add that object to the ITILRow
                for (int colNum=1; colNum<=numCols; colNum++) {
                    Object o = rs.getObject(colNum);

                    // JDBC4Array is a real pain in the butt
                    ArrayList<Integer> tmpList = new ArrayList<Integer>();
                    if (o != null) {
                        if (o.getClass().getSimpleName().endsWith("Array")) {
                            // At least at this time, these Arrays are all Integer[]
                            Array a = (Array) o;
                            Integer[] ints = (Integer[]) a.getArray();
                            for (Integer i : ints) {
                                tmpList.add(i);
                            }
                            o = tmpList;
                        }
                    }

                    row.add(o);
                }

                // Add the ITILRow to allRows
                allRows.add(row);
            }

Then, in the calling method...

    for (ITILRow row : allRows) {
        ...
        ArrayList comps = (ArrayList) row.getObject(5);
        Integer[] argh = (Integer[]) ((ArrayList<Integer>) comps).toArray();

        ...
    }

And I get:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;

Help would be appreciated. I've gotten my brain tied into a knot on this one.

Thanks,

Ringworm answered 27/2, 2013 at 22:10 Comment(0)
N
34

List#toArray() returns an Object array. Use List#toArray(T[]) instead.

Integer[] arg = (Integer[]) comps.toArray(new Integer[comps.size()]);
Nations answered 27/2, 2013 at 22:11 Comment(5)
Holy smokes you were fast. Thanks that did it. Yay! I'm moving forward again!! Thank you thank you thank you.Ringworm
I think the parameter should be new Integer[0] -- you don't actually need any space in the object passed in, it is only used to get the class info, but you must instantiate it.Madagascar
@AgilePro: If the passed array is large enough (at least comps.size() elements), the passed array will be filled and returned by the toArray method. Making the array large enough will prevent an additional array of the same type to created.Ranunculaceous
Oh. That makes sense. And then, yes, it is better to make one of the exact size. However, the "new" is missing above.Madagascar
The casting to (Integer[]) is even redundant this way. Nice solution !Anaplasty

© 2022 - 2024 — McMap. All rights reserved.