classcastexception

JSF and type safety

佐手、 提交于 2019-11-26 21:40:34
问题 As I struggled for hours I finally found where those annoying ClassCastException s came from, which I thought were produced by Hibernate and it's enum -mapping. But they came from my JSF view, where I passed a List from <h:selectManyCheckbox value="#{createUserManager.user.roles}" ... > <f:selectItems value="#{createUserManager.roles}"/> </h:selectManyCheckbox> back into my backing bean. My data simply consists of the values of an enum: public Role[] getRoles() { return Role.values(); } . I

Why does it compile when casting to an unrelated interface?

孤者浪人 提交于 2019-11-26 19:05:49
interface Printable {} class BlackInk {} public class Main { public static void main(String args[]) { Printable printable = null; BlackInk blackInk = new BlackInk(); printable = (Printable)blackInk; } } If the preceding code is compiled and run, the result is a ClassCastException at printable = (Printable)blackInk; . But, if Printable is changed to a class, it doesn't compile because blackInk can't be cast to Printable. Why does it compile when Printable is an interface? The compiler does not know that this won't work: You could have a subclass of BlackInk that implements Printable. Then the

Getting class cast exception where both classes are exactly the same

不想你离开。 提交于 2019-11-26 19:01:14
I am doing a JBoss SEAM project and when I view a form I get this error. java.lang.ClassCastException: it.cogitoweb.csi.entity.csiorelav.CsiTipoLav cannot be cast to it.cogitoweb.csi.entity.csiorelav.CsiTipoLav Its alway the same JPA class which is related to the form which is shown on the screen, it doesn't make sense to me why is it the same class, it seems impossible. This happens when two different ClassLoader objects load classes with the same name. The equality of two classes in Java depends on the fully qualified name and the class loader that loaded it. So if two independent class

Solution for the ClassCastException due to ClassLoader issue

北战南征 提交于 2019-11-26 18:29:14
问题 I have two ClassLoaders which loads the same class. So, obviously these can't cast to one another. But I need to access an object created in the other ClassLoader. I have access to both ClassLoaders. How can I use that object in the other class? I don't need to cast the object to match to the current ClassLoader. But the issue is that the returned object's type is Object . So, I have to cast down that object to access some methods. How can I do that? Normal cast like the following causes

Access Array column in Spark

会有一股神秘感。 提交于 2019-11-26 14:16:50
问题 A Spark DataFrame contains a column of type Array[Double]. It throw a ClassCastException exception when I try to get it back in a map() function. The following Scala code generate an exception. case class Dummy( x:Array[Double] ) val df = sqlContext.createDataFrame(Seq(Dummy(Array(1,2,3)))) val s = df.map( r => { val arr:Array[Double] = r.getAs[Array[Double]]("x") arr.sum }) s.foreach(println) The exception is java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot be

java: (String[])List.toArray() gives ClassCastException

感情迁移 提交于 2019-11-26 11:14:52
The following code (run in android) always gives me a ClassCastException in the 3rd line: final String[] v1 = i18nCategory.translation.get(id); final ArrayList<String> v2 = new ArrayList<String>(Arrays.asList(v1)); String[] v3 = (String[])v2.toArray(); It happens also when v2 is Object[0] and also when there are Strings in it. Any Idea why? This is because when you use toArray() it returns an Object[], which can't be cast to a String[] (even tho the contents are Strings) This is because the toArray method only gets a List and not List<String> as generics are a source code only thing, and not

Meaning of java.lang.ClassCastException: someClass incompatible with someClass

巧了我就是萌 提交于 2019-11-26 09:53:49
问题 I was experiencing occasional exceptions in XPages application: java.lang.ClassCastException: someClass incompatible with someClass. Both mentioned classes are the same, it is class used as session bean. I was not able to google anything covering my problem. Usual explanation for this was change in design elements, not my case. The XPage application become unusable (pages using session bean someClass) since that moment, until restart of http task, or resave of faces-config.xml. In some cases

Generics, arrays, and the ClassCastException

被刻印的时光 ゝ 提交于 2019-11-26 09:47:41
问题 I think there must be something subtle going on here that I don\'t know about. Consider the following: public class Foo<T> { private T[] a = (T[]) new Object[5]; public Foo() { // Add some elements to a } public T[] getA() { return a; } } Suppose that your main method contains the following: Foo<Double> f = new Foo<Double>(); Double[] d = f.getA(); You will get a CastClassException with the message java.lang.Object cannot be cast to java.lang.Double . Can anyone tell me why? My understanding

UISelectMany on a List<T> causes java.lang.ClassCastException: java.lang.String cannot be cast to T

孤人 提交于 2019-11-26 08:35:52
问题 I am using <p:selectCheckboxMenu> on a List<Long> : <p:selectCheckboxMenu value=\"#{bean.selectedItems}\"> <f:selectItems value=\"#{bean.availableItems}\" /> </p:selectCheckboxMenu> private List<Long> selectedItems; private Map<String, Long> availableItems; When submitting the form and looping over the selected items as below, for (int i = 0; i < selectedItems.size(); i++) { Long id = selectedItems.get(i); // ... } Then I get a class cast exception: java.lang.ClassCastException: java.lang

Why does it compile when casting to an unrelated interface?

非 Y 不嫁゛ 提交于 2019-11-26 08:30:50
问题 interface Printable {} class BlackInk {} public class Main { public static void main(String args[]) { Printable printable = null; BlackInk blackInk = new BlackInk(); printable = (Printable)blackInk; } } If the preceding code is compiled and run, the result is a ClassCastException at printable = (Printable)blackInk; . But, if Printable is changed to a class, it doesn\'t compile because blackInk can\'t be cast to Printable. Why does it compile when Printable is an interface? 回答1: The compiler