All Products
Search
Document Center

Mobile Testing:java_lang_ClassCastException

Last Updated:Feb 28, 2022

Problem description

This exception occurs when you perform forced type conversion but the converted object is not the instance where type conversion occurs.

Solution

This exception occurs when you perform forced type conversion but the converted object is not the instance where type conversion occurs. You can execute the if(object instanceof) statement to determine whether the type of an object can be forced to convert.

Sample code

   public Son getSon(Father reference){
      if(reference instanceof Son){
        Son son=(Son)reference;
        return son;
      }
      return null;
    }

References