1
com.envers.DuaVO@5d8a2977
Exception in thread "main" java.lang.ClassCastException: com.envers.DuaVO cannot be cast to [Ljava.lang.Object;

I'm getting the above exception when I run the code. What am I missing here? Any help would be appreciated.

for (Object[] revtypes : duaRevType) {
        System.out.println("  ");
        System.out.println(revtypes[0]);

        //Printing Out DUA Values
        Object[] objArray = (Object[]) revtypes[0];
        DuaVO dduaVo = (DuaVO) objArray[0];

        System.out.println("Dua Number: " + dduaVo.getDuaNumber());
        System.out.println("Dua Short Description: " + dduaVo.getDuaShortDesc());

        System.out.println("This DUA was modified on: " + revtypes[1]);
        System.out.println("Revision Type: " + revtypes[2]);

// Console Output

    com.envers.DuaVO@2d4c8822  // I'm trying to print this object
  This DUA was modified on: DefaultRevisionEntity(id = 499,     revisionDate   Mar 16, 2015 11:36:38 AM)
  Revision Type: ADD
AppSensei
  • 8,270
  • 22
  • 71
  • 99

4 Answers4

2

Youre attempting to cast non-compatible types. Perhaps you meant

DuaVO dduaVo = (DuaVO) revtypes[0];

instead of

Object[] objArray = (Object[]) revtypes[0];
DuaVO dduaVo = (DuaVO) objArray[0];
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

You are casting single object into object array - here is our class cast excepton

Object[] objArray = (Object[]) revtypes[0];

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • 1
    Your code is not really an answer - the sentence is. Leaving your answer like that will make it confusing as you are coding the error but not the answer. I would suggest coding the answer as well. – blurfus Mar 24 '15 at 20:27
1
java.lang.ClassCastException: com.envers.DuaVO cannot be cast to [Ljava.lang.Object

[Ljava.lang.Object is the compiler's obtuse way of printing Object[]. The error message is saying that DuaVO cannot be cast to Object[].

My guess is you can simplify the two assignments to:

DuaVO dduaVo = (DuaVO) revtypes[0];
Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

This is my take on it:

// for each array of objects (named revtypes) in your class duaRevType
for (Object[] revtypes : duaRevType) {
        System.out.println("  ");
        // print the first element
        System.out.println(revtypes[0]);

        //Printing Out DUA Values
        //->cast the first element of your Object array
        DuaVO dduaVo = (DuaVO) revtypes[0];
        ...
 }
blurfus
  • 13,485
  • 8
  • 55
  • 61