Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException: java.lang.Object cannot be cast to java.lang.Integer

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,

like image 530
Lurk21 Avatar asked Feb 27 '13 22:02

Lurk21


People also ask

How do I resolve Java Lang ClassCastException error?

// type cast an parent type to its child type. In order to deal with ClassCastException be careful that when you're trying to typecast an object of a class into another class ensure that the new type belongs to one of its parent classes or do not try to typecast a parent object to its child type.

What is ClassCastException in Java?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

What is ClassCastException in Java with example?

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. So, for example, when one tries to cast an Integer to a String , String is not an subclass of Integer , so a ClassCastException will be thrown. Object i = Integer.

Is ClassCastException checked?

ClassCastException is one of the unchecked exception in Java. It can occur in our program when we tried to convert an object of one class type into an object of another class type.


1 Answers

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

Integer[] arg = (Integer[]) comps.toArray(new Integer[comps.size()]);
like image 125
PermGenError Avatar answered Oct 10 '22 07:10

PermGenError