实体不能转换为java.lang.Object;

时间:2015-03-24 20:23:28

标签: java

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

我在运行代码时遇到上述异常。我在这里错过了什么?任何帮助将不胜感激。

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]);

//控制台输出

    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

4 个答案:

答案 0 :(得分:2)

您试图投射不兼容的类型。也许你的意思是

DuaVO dduaVo = (DuaVO) revtypes[0];

而不是

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

答案 1 :(得分:1)

您正在将单个对象转换为对象数组 - 这是我们的类转换异常

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

答案 2 :(得分:1)

java.lang.ClassCastException: com.envers.DuaVO cannot be cast to [Ljava.lang.Object

[Ljava.lang.Object是编译器的obtuse way of printing Object[]。错误消息指出DuaVO无法转换为Object[]

我的猜测是你可以将两个作业简化为:

DuaVO dduaVo = (DuaVO) revtypes[0];

答案 3 :(得分:0)

这是我的看法:

// 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];
        ...
 }