Tags down


ClassCastException when trying to cast a subclass

By : user2175494
Date : October 15 2020, 08:10 AM
wish helps you You are creating one object of type B. As per your class hierarchy, each B object is also an A, but not a C.
Now let's look on how to assign the reference to this B object to various variables:
code :
Object o = new B(); // You can always assign references to an Object-typed variable.
A a = (A) o; // This works because the object is of type A.
B b = (B) o; // This works because the object is of type B.
C c = (C) o; // This does not work because the object is not of type C.

Share : facebook icon twitter icon

How to solve cast issue java.lang.ClassCastException: $Proxy cannot be cast to NotEmpty wich is annotation

By : jvu009
Date : March 29 2020, 07:55 AM
With these it helps I want to thank you each for spending time on this issue.
The error comes from others implemented annotations that have this constraint validator. @Constraint(validatedBy = {INotEmptyValidator.class}). This was done for sure by mistake.

ClassCastException when trying to cast FrameLayout to subclass

By : Ujas Surani
Date : March 29 2020, 07:55 AM
wish helps you You aren't using your MyCustomView anywhere.
When you call super.onCreateView(inflater, container, savedInstanceState);, you are letting the base class (ListFragment) handle creating the layout for your fragment, and the framework's ListFragment class has no idea that you created a MyCustomView or how to use it.
code :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getActivity(), android.R.layout.simple_list_item_1,
            new String[]{ "one", "two", "three"});

    View layout = inflater.inflate(R.layout.list_fragment_layout, container, false);
    MyCustomView myView = (MyCustomView) layout.findViewById(R.id.custom_view_id);

    return layout;

Exception java.lang.classCastException when cast subclass from SuperClass

By : Nenad Nikolic
Date : March 29 2020, 07:55 AM
Hope that helps You can convert a Moto to a Vehiculo without the need to check anything and the need to cast it as Moto is a sub class of Vehiculo but if you want to do the opposite you need to check first if your instance of Vehiculo is an instance of Moto using instanceof because an instance of Vehiculo is not necessary an instance of Moto which is root cause your problem here as you get a ClassCastException. Even worse here, according to your error message you try to cast an instance of Vehiculo to an instance of Moto which is simply not possible.
code :
if (vehiculos[pos] instanceof Moto) {
    // Here we can safety cast to a moto
    Moto moto = (Moto) vehiculos[pos];
public String getInfoDerivada(int pos){
    if(pos < 0 || pos >= cont){
        return null;
    if(vehiculos[pos] instanceof Coche){
        return ((Coche)vehiculos[pos]).getColor();
    } else if(vehiculos[pos] instanceof Moto){
        return String.valueOf(((Moto)vehiculos[pos]).getPotencia());
    } else if(vehiculos[pos] instanceof Camion){
        return String.valueOf(((Camion)vehiculos[pos]).getTonelaje());
    throw new IllegalStateException(
        String.format("Unknown type: %s", vehiculos[pos].getClass().getName())
public boolean addVehiculo (Vehiculo v){
        vehiculos[cont] = v;// instead of new Vehiculo(v);
public String getInfoDerivada(int pos){
    if(pos < 0 || pos >= cont){
        return null;
    return vehiculos[pos].getInfoDerivada();

If I've cast a subclass as its superclass, and call a method that was overridden in the subclass, does it perform the ov

By : homiehomes
Date : March 29 2020, 07:55 AM
hope this fix your issue The subclass method will be called. That's the beauty of polymorphism.

Subclass object needing to be type cast as the subclass to access subclass methods

By : Santosh
Date : March 29 2020, 07:55 AM
With these it helps
Why am I having to cast myAccount as a SavingsAccount when it's been declared as a SavingsAccount?
Related Posts Related Posts :
  • Kafka consumer returns no records
  • Timediff as resultset
  • Deadlock happens if I use lambda in parallel stream but it doesn't happen if I use anonymous class instead?
  • Check if object does throw a NullPointerException
  • I'm trying to understand how "calling an object's method" works in Java, but it's confusing me
  • Honeypot sample test Odd Numbers question
  • How to query Oracle via JDBC for an intersection
  • i keep getting an error: "testapp has stopped" when i go from Main2Activity.java to Main3Activity.java
  • JPA: Adding new entry to a many-to-many taking long time
  • Bouncy Castle PQC XMSS signing: NullPointerException after retrieving SecretKey from KeyStore
  • Extremely slow built-in AES encryption with Java
  • Remove duplicates items from arraylist and hashmap
  • Traversing a Table & Clicking Links Within Using Selenium
  • How to make a list of contents longer than the parent screen?
  • How to get tools.jar for OpenJDK 11 on Windows?
  • Java: HashMap Into Nested JSON Object
  • addLast method for a deque with only one sentinel node
  • Oracle datetime which does not exist in SQL Server
  • Thread's ThreadLocals cleaning
  • String index out of range: 0 Error For using RedirectAttributes in Spring Boot
  • How to pass an array to POST API?
  • What is the spring-boot-configuration-processor ? Why do people exclude libraries from it? Why is it invisible in depend
  • concurrency in a single Processor instance and concurrentConsumers
  • indexOf + substring in Java
  • TestNG.xml - New browser session does not open after first one is closed
  • How to deterministically map sequential integers to uniformly distributed doubles
  • Problem using oracle-serverjre:8 in dockerfile
  • PDF 'Itext User Agent' cache size and how to clear it
  • set proxy for firefox using sikuli in java
  • UnrecognizedPropertyException when using Jackson, @JsonIdentityInfo and @JsonDeserialize with a lombok Builder
  • Java Array containing reference to object
  • How can I add bundled jars to my classpath in an Ant build task?
  • cannot be cast to Java
  • SseEmitter with proxy
  • Microservices broadcast messages
  • Unable log in to the index.html
  • Simple Java 11 JavaFXprogram, Error when run from JAR file (Windows)
  • randomly displaying circles within gridPane cells in javaFX
  • How to force client to implement multiple generic interfaces with same type
  • Set transparent background on JList
  • How to print the Execution Plan for batch processing in Flink?
  • How to specify which version of a concrete implementation for an abstract class to autowire in Spring?
  • Regular expression get the third element from a string
  • How do I use client certificates in a client java application?
  • Allow dashes in email validations
  • Unable to display Json data
  • Add constraints to lines in javaFX
  • How do you include a classpath wildcard as an argument to a java bash call?
  • How to use classes from one project on another project in java?
  • How to parse a column that has a custom json format from a spark DataFrame
  • How to parse entire DOM elements and follow changes in DOM using selenium webdriver in java
  • java set object field equal another field upon instantation
  • Java Threads Object Lock
  • Euro Currency getting Question mark?
  • Running out of pools using CompleteableFuture and Spring Transaction
  • Drools can not use a generic type for a function's parameter in DRL
  • Create a new ArrayList with all the elements of another ArrayList except one in one line of code
  • log4j could not create log file in spring boot resti api
  • Android BLE discovery issue
  • Adding Java final keyword to working method that builds instances inside a loop
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org