cancel
Showing results for 
Search instead for 
Did you mean: 

Type Casting Of Super Class is Throwing Runtime Error in Java 1.6

akhil_mishra
Explorer
0 Kudos

Hi All,

I am facing Runtime Error while executing Custom code in Java 1.6, which was working fine with Java 1.4.

The issue is Typecasting Super Class Object to Sub Class Reference. Scenario is explained below:

Utils.Java ( Here we are doing Typecasting)

Z_BusinessObjectManager zOZBOM = new Z_BusinessObjectManager();


if (zOZBOM instanceof BOManager) {

BusinessObjectManager zOBOM = (BusinessObjectManager) userSessionData.getBOM(BusinessObjectManager.ISACORE_BOM);

return (Z_BusinessObjectManager) zOBOM; -- This is throwing Runtime Error - ClassCastException

}

Z_BusinessObjectManager.java (Here we are doing Inheritance)


public class Z_BusinessObjectManager extends BusinessObjectManager


{

public Z_BusinessObjectManager() {


super();

}​

Stack Trace:

[EXCEPTION]

java.lang.ClassCastException: class com.sap.isa.businessobject.BusinessObjectManager:sap.com/b2b_ecom@com.sap.engine.boot.loader.ResourceMultiParentClassLoader@5f9d3ee6@alive incompatible with class com.ecom.isa.businessobject.Z_BusinessObjectManager:sap.com/b2b_ecom@com.sap.engine.boot.loader.ResourceMultiParentClassLoader@5f9d3ee6@alive

#Please assume imports and other set up is all fine. Only issue is Typecast.

What I have already tried is below:

First Assigning Sub Class Object to Super Class Reference. Then saving the retrun output from getBOM method to this Super class Object and then typecasting it. No use, same error.

instanceOf Operator is also not helping to prevent this code to execute -> means zOZBOM instanceof BOManager is returning true, which makes it further weird. Above of all, same code, same type cast is working fine in SAP 7.0 environment (JDK 1.4_19) where as throwiong exception in SAP 7.3 environment (JDK 1.6).

Any suggestion how to avoid this exception either by code or by any config change in Java Compiling or Runtime Environment Or any work around to achieve same functionality without error will be of great help.

- Akhil


Accepted Solutions (0)

Answers (2)

Answers (2)

akhil_mishra
Explorer
0 Kudos

FYI. IN bom-config.xml, the ZBOM Object was instantiated instead of BOM, which fixes the Type Casting Issues.

Ryan-Crosby
Active Contributor
0 Kudos

Hi Akhil,

The trouble with the exception is that the object in question is never a Z_BusinessObjectManager in any scope of execution of that code block so it cannot be cast to the sub class.  If you want that code block to execute then I would make the following adjustment:

Z_BusinessObjectManager zOZBOM = new Z_BusinessObjectManager();

Z_BusinessObjectManager zOBOM;
if (zOZBOM instanceof BOManager) {

zOBOM = (BusinessObjectManager) userSessionData.getBOM(BusinessObjectManager.ISACORE_BOM);

return (Z_BusinessObjectManager) zOBOM;

}


Regards,

Ryan Crosby



akhil_mishra
Explorer
0 Kudos

Hi Ryan,

In following above suggestion, the NetWeaver gives Type mismatch Error, which seems ok to me as

zOBOM is reference of type Z_BusinessObjectManager

where as Object returned by (BusinessObjectManager) userSessionData.getBOM(BusinessObjectManager.ISACORE_BOM) is of BusinessObjectManager.

Hence type mismatch.

If such assignment is possible, at first place, then typecasting is not needed all together, isn't?

Thanks for your inputs!

Ryan-Crosby
Active Contributor
0 Kudos

Hi Akhil,

If Z_BusinessObjectManager extends BusinessObjectManager then there should be no type mismatch error given by the system.  The one other change that could be made also would be to remove the cast on the return statement as zOBOM is already declared as a Z_BusinessObjectManager.

Regards,

Ryan Crosby