Questions tagged [classcastexception]

The Java exception thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

ClassCastException is the Java unchecked exception thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

FAQ:

  1. Explanation of "ClassCastException" in Java
  2. What else can throw a ClassCastException in java?
1609 questions
8
votes
2 answers

Is there Scala equivalent of C# "as" keyword?

Is there Scala equivalent of C# as keyword? var something = obj as MyClass; Scala's asInstanceOf throws java.lang.ClassCastException: val something = obj.asInstanceOf[MyClass]
TN.
  • 17,602
  • 25
  • 84
  • 141
8
votes
5 answers

shouldn't this code produce a ClassCastException

The following code compiles and runs successfully without any exception import java.util.ArrayList; class SuperSample {} class Sample extends SuperSample { @SuppressWarnings("unchecked") public static void main(String[] args) { try { …
Thirumalai Parthasarathi
  • 4,251
  • 1
  • 22
  • 42
8
votes
2 answers

Android ClassCastException with blank type

I am getting a strange behavior, and I guess I'm more looking for an explanation than a solution (although solution is welcome as well!). Here's the code: PackageManager pm = context.getPackageManager(); List pkgList =…
8
votes
3 answers

Servlet.service() throws ClassCastException

I have a java web application. I have set up this new project and getting the following exception when I run a jsp, Oct 10, 2012 3:01:49 AM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet jsp threw…
Rishabh
  • 328
  • 1
  • 3
  • 16
8
votes
2 answers

Iterate parameterized List (after raw List type assignment)

Language: Java Compiler version: 1.6 In the below code, am trying to do the following: create a List add a String assign List to raw List create a List assign the raw List to List add an Integer retrieve the value…
pulikarthi
  • 117
  • 1
  • 1
  • 6
8
votes
1 answer

ClassCastException while dynamically loading a class in Android

I have a thread that loads different classes for the resources it needs depending on the specific implementation of the system. My implementation is on Android and I have a class that returns the specific classes needed by my implementation. I seem…
Marakatu
  • 121
  • 2
  • 7
7
votes
1 answer

java.lang.ClassCastException: android.app.Application cannot be cast to greendroid.app.GDApplication

I'm getting a class cast exception namely java.lang.ClassCastException: android.app.Application cannot be cast to greendroid.app.GDApplication when I'm trying to use the green droid library in my project.Here is the concerned code. I've been trying…
Dinesh Venkata
  • 1,069
  • 1
  • 9
  • 21
7
votes
3 answers

How to set width and height for custom view in programmatically?

I have created a custom view named MyDraw ,this is my MyDraw code, public class MyDraw extends View { public MyDraw(Context context) { super(context); } public MyDraw(Context context, AttributeSet attrs, int defStyle) { …
SuReSh PaTi
  • 1,345
  • 6
  • 28
  • 46
7
votes
3 answers

Unexplainable ClassCastException thrown in Play Framework

I've been experiencing a strange error while working on my Play Framework project. While my project is running, I will sometimes receive a ClassCastException, but the error is this: ClassCastException occured : models.Person cannot be cast to…
torourke
  • 1,649
  • 2
  • 11
  • 9
7
votes
5 answers

Solution for the ClassCastException due to ClassLoader issue

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…
ravana
  • 157
  • 1
  • 1
  • 9
7
votes
1 answer

ClassCastException when converting from String to Object.. why?

I am just playing with MessageFormat but when I try to pass a String to MessageFormat format method it compiles fine but then I get a runtime classcast exception. Here is the code. MessageFormat format = new MessageFormat(""); Object obj = …
7
votes
2 answers

ClassCastException in SpringBoot Logging

I'm getting an issue when my application starts up. The application fails when configuring log levels, on a ClassCastException java.util.HashMap cannot be cast to org.springframework.boot.logging.LogLevel. I've tried setting logging.level.root=INFO…
xandermonkey
  • 2,775
  • 2
  • 22
  • 39
7
votes
2 answers

If A extends B extends C, why can I cast to A but get a ClassCastException casting to C?

I am trying to read an ASN1 object using Bouncycastle on Android. I expect it to be a DERSequence, which in Bouncycastle is a subclass of ASN1Sequence, which is a subclass of ASN1Object. import org.bouncycastle.asn1.ASN1InputStream; import…
user79758
7
votes
1 answer

Java8 Lambda Deserialization - ClassCastException

ClassCastException is thrown by Java8 upon deserializing a lambda when following conditions are met: Parent class has a method, reference to which is used to automatically create a Serializable lambda There are several child classes that extend it…
7
votes
4 answers

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String

when I am running this code its giving me exception java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String on q1.list(); Query q=session.createQuery("select categoryData FROM SearchHistoryEntity where…
u12345
  • 369
  • 2
  • 7
  • 16