java.lang.ClassCastException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3511169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:24:36  来源:igfitidea点击:

java.lang.ClassCastException

java

提问by selvam

Normally whats the reason to get java.lang.ClassCastException ..? I get the following error in my application

通常是什么原因得到 java.lang.ClassCastException ..?我的应用程序中出现以下错误

java.lang.ClassCastException: [Lcom.rsa.authagent.authapi.realmstat.AUTHw

采纳答案by Lauren?iu Dasc?lu

According to the documentation:

根据文档

Thrown to indicate that the code has attempted to cast an Objectto a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

抛出以指示代码已尝试将 anObject强制转换为它不是其实例的子类。例如,以下代码生成一个ClassCastException

Object x = new Integer(0);
System.out.println((String)x); 

回答by Jon Skeet

It's because you're casting to the wrong thing - you're trying to convert to a particular type, and the object that your express refers to is incompatible with that type. For example:

这是因为您正在转换为错误的东西 - 您正在尝试转换为特定类型,并且您的 express 引用的对象与该类型不兼容。例如:

Object x = "this is a string";
InputStream y = (InputStream) x; // This will throw ClassCastException

If you could provide a code sample, that would really help...

如果你能提供一个代码示例,那真的很有帮助......

回答by PaulJWilliams

ClassA a = <something>;
ClassB b = (ClassB) a;

The 2nd line will fail if ClassA is not a subclass of ClassB, and will throw a ClassCastException.

如果 ClassA 不是 ClassB 的子类,第二行将失败,并抛出 ClassCastException。

回答by Stephen C

@Lauren?iu Dasc?lu's answer explains how / why you get a ClassCastException.

@Lauren?iu Dasc?lu 的回答解释了您如何/为什么会收到 ClassCastException。

Your exception message looks rather suspicious to me, but it might help you to know that "[Lcom.rsa.authagent.authapi.realmstat.AUTHw" means that the actual type of the object that you were trying to cast was com.rsa.authagent.authapi.realmstat.AUTHw[]; i.e. it was an array object.

您的异常消息对我来说看起来很可疑,但它可能会帮助您了解“[Lcom.rsa.authagent.authapi.realmstat.AUTHw”意味着您尝试投射的对象的实际类型是com.rsa.authagent.authapi.realmstat.AUTHw[];即它是一个数组对象。

Normally, the next steps to solving a problem like this are:

通常,解决此类问题的下一步是:

  • examining the stacktrace to figure out which line of which class threw the exception,
  • examining the corresponding source code, to see what the expected type, and
  • tracing back to see where the object with the "wrong" type came from.
  • 检查堆栈跟踪以找出哪个类的哪一行引发了异常,
  • 检查相应的源代码,看看预期的类型,以及
  • 回溯以查看具有“错误”类型的对象的来源。

回答by mR_fr0g

A ClassCastExceptionocurrs when you try to cast an instance of an Object to a type that it is not. Casting only works when the casted object follows an "is a" relationship to the type you are trying to cast to. For Example

一个ClassCastException当您尝试转换为一个类型的对象的实例,它不是ocurrs。仅当被转换的对象与您尝试转换为的类型遵循“是”关系时,转换才有效。例如

Apple myApple = new Apple();
Fruit myFruit = (Fruit)myApple;

This works because an apple 'is a' fruit. However if we reverse this.

这是有效的,因为苹果“是”水果。但是,如果我们扭转这一点。

Fruit myFruit = new Fruit();
Apple myApple = (Apple)myFruit;

This will throw a ClasCastException because a Fruit is not (always) an Apple.

这将抛出 ClasCastException 因为 Fruit 不是(总是)Apple。

It is good practice to guard any explicit casts with an instanceofcheck first:

最好instanceof先通过检查来保护任何显式强制转换:

if (myApple instanceof Fruit) {
  Fruit myFruit = (Fruit)myApple;
}

回答by Pushpraj Mehta

To avoid x !instance of Longprob Add

为了避免x !instance of Long问题添加

<property name="openjpa.Compatibility" value="StrictIdentityValues=false"/>

in your persistence.xml

在你的persistence.xml