TreeSet and ClassCastException
Hi,
I know that I shouldn't add abjects to TreeSet which can't be compared (it's called
mutually comparable, isn't it ?) - so basically thay all have to be the same type.
But could anybody explain me why if I break this tule I get this error:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer
at java.lang.String.compareTo(String.java:90)
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.put(TreeMap.java:465)
at java.util.TreeSet.add(TreeSet.java:210)
at com.adrian.Book.main(Book.java:26)
What interests me here is what a TreeMap is doing here ?
And how am I supposed to read this information (step by step) ?
I don't understand this output ...
Here is the code:
public class Book {
public static void main(String[] args) {
Set s=new TreeSet();
s.add(new Integer(1));
s.add("a");
s.add(new Integer(5));
s.add(new Integer(3));
for(Object o:s)
System.out.print(o+" ");
}Thanks,
Adrian
Ok - I found that TreeSet uses TreeMap in its
implementation - that's why it appears
there.That is correct.
TreeSet makes a call to TreeMap's put(...) method on line 210 from the add(...) method.
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer
at java.lang.String.compareTo(String.java:90)
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.put(TreeMap.java:465)
at java.util.TreeSet.add(TreeSet.java:210)
at com.adrian.Book.main(Book.java:26)
If you're curious what is happening exactly, you can always (download and) take a look at the source code:
http://download.java.net/jdk6/
Similar Messages
-
Query regarding implementation of HashSet and TreeSet and AbstractSet
I have found that the treeSet and HashSet extends AbstractSet and implements Cloneable and Serializable interfaces.
What happens if AbstractSet itself implements Cloneable and Serializable interfaces, so that The HashSet and TreeSet need not to implement separately?
Is it a design concern or is there any logic behind in doing so?
I appreciate your quick response..
Thanks
Shiva kumarIt would mean that anybody else such as me who extended AbstractSet would be stuck with my implementation being serializable and cloneable whether I wanted or not. No doubt the designers wanted to provide the freedom not to do so.
-
I am trying to implement a schedule using a TreeSet and a Comparator.
The TreeSet constructor receives a Comparator.
Then I start adding objects that implement an Interface called ScehdulableInterface.
All objects that implement this interface have a method:
Public Date getDate()
The Comparator compare method is defined like this:
public int compare(Object o1, Object o2) {
return ((SchedulableInterface)o1).getEndDate().compareTo(((SchedulableInterface)o2).getEndDate());
My problem is the TreeSet is not accepting objects in the same date. Is there any other Collection that accepts the Comparator interface and accepts objects with the same date ?
Thanks.
EliOf course with something like this:public int compare(Object o1, Object o2) {
// checks for null and other error handling omitted...
SchedulableInterface si1 = (SchedulableInterface) o1;
SchedulableInterface si2 = (SchedulableInterface) o2;
int endDateCompare = s1.getEndDate().compareTo(s2.getEndDate();
if (endDateCompare == 0) {
return s1.getSubject().compareTo(s2.getSubject());
} else {
return endDateCompare; -
NegativeArraySizeException and ClassCastException
Guys,could someone give me a scenario where NegativeArraySizeException and ClassCastException are thrown???
int myArraySize = -1;
int[] myArray = new int[myArraySize];
Object someObject = "foo";
Integer anInteger = (Integer) someObject; -
WSAD 5, xerces, datasource and classcastexception issue
I am running into an issue trying to run my code once I upgraded from WDAD 5.0 to WSAD 5.01. I am getting the error below when I try and connect to my DataSource. This all was working fine before I upgraded. It looks like an issue with the xerces.jar file because if I take out the JAXP stuff in the xerces.jar file I work fine. But I need the JAXP stuff because I am doing messaging. Anyone have any ideas?
[5/22/03 7:02:59:848 CDT] 2ec4c80 SystemOut O 2003.05.22 07:02:59.848--ServerSession(426724481)--Thread[asyncDelivery0,5,main]--Connection(47762586)--TopLink, version:TopLink - 9.0.3 (Build 423)
2003.05.22 07:02:59.848--ServerSession(426724481)--Thread[asyncDelivery0,5,main]--Connection(47762586)--connecting(DatabaseLogin(
platform => DB2Platform
user name => "ou79ecm"
datasource name => "jdbc/DB239DataSource"
[5/22/03 7:03:00:118 CDT] 2ec4c80 ConnectionFac I J2CA0122I: Resource reference jdbc/DB239DataSource could not be located, so default values of the following are used: [Resource-ref settings]
res-auth: 1 (APPLICATION)
res-isolation-level: 0 (TRANSACTION_NONE)
res-sharing-scope: true (SHAREABLE)
res-resolution-control: 999 (undefined)
[Other attributes]
isCMP1_x: false (not CMP1.x)
isJMS: false (not JMS)
[5/22/03 7:03:01:160 CDT] 2ec4c80 ConnectionFac E J2CA0009E: An exception occurred while trying to instantiate the ManagedConnectionFactory class com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl used by resource jdbc/DB239DataSource : java.lang.ClassCastException: org.apache.xerces.jaxp.SAXParserFactoryImpl
at javax.xml.parsers.SAXParserFactory.newInstance(Unknown Source)
at com.ibm.ejs.j2c.XMLReader.validate(XMLReader.java:673)
at com.ibm.ejs.j2c.XMLReader.init(XMLReader.java:241)
at com.ibm.ejs.j2c.XMLReader.<init>(XMLReader.java:165)
at com.ibm.ejs.j2c.ConnectorRuntime.getJ2C_Properties(ConnectorRuntime.java:341)
at com.ibm.ejs.j2c.poolmanager.PoolManager.<init>(PoolManager.java:541)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createCFD_Common(ConnectionFactoryBuilderImpl.java:699)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createConnectionFactoryDetails(ConnectionFactoryBuilderImpl.java:417)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.getObjectInstance(ConnectionFactoryBuilderImpl.java:1400)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:313)
at com.ibm.ws.naming.util.Helpers.processSerializedObjectForLookup(Helpers.java:884)
at com.ibm.ws.naming.jndicos.CNContextImpl.processResolveResults(CNContextImpl.java:1675)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1538)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1458)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:1130)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:122)
at javax.naming.InitialContext.lookup(InitialContext.java:363)
at oracle.toplink.jndi.JNDIConnector.connect(Unknown Source)
at oracle.toplink.sessions.DatabaseLogin.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.buildConnection(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.startUp(Unknown Source)
at oracle.toplink.threetier.ServerSession.connect(Unknown Source)
at oracle.toplink.publicinterface.DatabaseSession.login(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getSession(ToplinkServerSessionFactory.java:121)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getClientSession(ToplinkServerSessionFactory.java:56)
at com.deere.jdc.component.rcts.dao.RCTSGateway.findDealerASMByDealerNumber(RCTSGateway.java:194)
at com.deere.jdc.component.rcts.service.RCTSService.getRCTSDealerASM(RCTSService.java:159)
at com.deere.jdc.component.rcts.adapter.SoapRCTSService.getRCTSDealerASM(SoapRCTSService.java:107)
at java.lang.reflect.Method.invoke(Native Method)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.invokeServiceMethod(RCTSMessageHandler.java:103)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.processMessage(RCTSMessageHandler.java:62)
at com.deere.jdc.mqseries.MQJMSMessageHandler.onMessage(MQJMSMessageHandler.java:122)
at com.ibm.mq.jms.MQQueueReceiver.receiveAsync(MQQueueReceiver.java:861)
at com.ibm.mq.jms.SessionAsyncHelper.run(SessionAsyncHelper.java:401)
at java.lang.Thread.run(Thread.java:513)
[5/22/03 7:03:01:310 CDT] 2ec4c80 ConnectionFac A J2CA0013I: An exception occurred while trying to create ManagedConnectionFactory for DB239DataSource : java.lang.ClassCastException: org.apache.xerces.jaxp.SAXParserFactoryImpl
at javax.xml.parsers.SAXParserFactory.newInstance(Unknown Source)
at com.ibm.ejs.j2c.XMLReader.validate(XMLReader.java:673)
at com.ibm.ejs.j2c.XMLReader.init(XMLReader.java:241)
at com.ibm.ejs.j2c.XMLReader.<init>(XMLReader.java:165)
at com.ibm.ejs.j2c.ConnectorRuntime.getJ2C_Properties(ConnectorRuntime.java:341)
at com.ibm.ejs.j2c.poolmanager.PoolManager.<init>(PoolManager.java:541)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createCFD_Common(ConnectionFactoryBuilderImpl.java:699)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createConnectionFactoryDetails(ConnectionFactoryBuilderImpl.java:417)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.getObjectInstance(ConnectionFactoryBuilderImpl.java:1400)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:313)
at com.ibm.ws.naming.util.Helpers.processSerializedObjectForLookup(Helpers.java:884)
at com.ibm.ws.naming.jndicos.CNContextImpl.processResolveResults(CNContextImpl.java:1675)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1538)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1458)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:1130)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:122)
at javax.naming.InitialContext.lookup(InitialContext.java:363)
at oracle.toplink.jndi.JNDIConnector.connect(Unknown Source)
at oracle.toplink.sessions.DatabaseLogin.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.buildConnection(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.startUp(Unknown Source)
at oracle.toplink.threetier.ServerSession.connect(Unknown Source)
at oracle.toplink.publicinterface.DatabaseSession.login(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getSession(ToplinkServerSessionFactory.java:121)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getClientSession(ToplinkServerSessionFactory.java:56)
at com.deere.jdc.component.rcts.dao.RCTSGateway.findDealerASMByDealerNumber(RCTSGateway.java:194)
at com.deere.jdc.component.rcts.service.RCTSService.getRCTSDealerASM(RCTSService.java:159)
at com.deere.jdc.component.rcts.adapter.SoapRCTSService.getRCTSDealerASM(SoapRCTSService.java:107)
at java.lang.reflect.Method.invoke(Native Method)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.invokeServiceMethod(RCTSMessageHandler.java:103)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.processMessage(RCTSMessageHandler.java:62)
at com.deere.jdc.mqseries.MQJMSMessageHandler.onMessage(MQJMSMessageHandler.java:122)
at com.ibm.mq.jms.MQQueueReceiver.receiveAsync(MQQueueReceiver.java:861)
at com.ibm.mq.jms.SessionAsyncHelper.run(SessionAsyncHelper.java:401)
at java.lang.Thread.run(Thread.java:513)
[5/22/03 7:03:01:340 CDT] 2ec4c80 Helpers W NMSV0605W: A Reference object looked up from the context "localhost/nodes/localhost/servers/server1" with the name "jdbc/DB239DataSource" was sent to the JNDI Naming Manager and an exception resulted. Reference data follows:
Reference Factory Class Name: com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl
Reference Factory Class Location URLs: <null>
Reference Class Name: DB239DataSource
Type: connectorName
Content: DB239DataSource
Type: classpath
Content: C:\Program Files\IBM\WebSphere Studio\runtimes\base_v5/lib/rsadapter.rar
Type: FactoryJndiName
Content: jdbc/DB239DataSource
Address Type: dd
AddressContents: ffffffac ffffffed 0 5 73 72 0 25 63 6f 6d 2e 69 62 6d 2e 65 6a 73 2e 6a 32 63 2e 52 65 73 6f 75 72 63 65 ...
Address Type: poolProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 27 63 6f 6d 2e 69 62 6d 2e 65 6a 73 2e 6a 32 63 2e 43 6f 6e 6e 65 63 74 6f ...
Address Type: configProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 23 63 6f 6d 2e 69 62 6d 2e 65 6a 73 2e 6a 32 63 2e 43 6f 6e 6e 65 63 74 6f ...
Address Type: dsProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 14 6a 61 76 61 2e 75 74 69 6c 2e 50 72 6f 70 65 72 74 69 65 73 39 12 ffffffd0 7a ...
Address Type: mcfProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 2d 63 6f 6d 2e 69 62 6d 2e 65 6a 73 2e 6a 32 63 2e 4d 61 6e 61 67 65 64 43 ...
Address Type: mbeanProps
AddressContents: ffffffac ffffffed 0 5 73 72 0 1a 63 6f 6d 2e 69 62 6d 2e 65 6a 73 2e 6a 32 63 2e 4d 42 65 61 6e 50 72 6f ...
Exception data follows:
java.lang.ClassCastException: org.apache.xerces.jaxp.SAXParserFactoryImpl
at javax.xml.parsers.SAXParserFactory.newInstance(Unknown Source)
at com.ibm.ejs.j2c.XMLReader.validate(XMLReader.java:673)
at com.ibm.ejs.j2c.XMLReader.init(XMLReader.java:241)
at com.ibm.ejs.j2c.XMLReader.<init>(XMLReader.java:165)
at com.ibm.ejs.j2c.ConnectorRuntime.getJ2C_Properties(ConnectorRuntime.java:341)
at com.ibm.ejs.j2c.poolmanager.PoolManager.<init>(PoolManager.java:541)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createCFD_Common(ConnectionFactoryBuilderImpl.java:699)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createConnectionFactoryDetails(ConnectionFactoryBuilderImpl.java:417)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.getObjectInstance(ConnectionFactoryBuilderImpl.java:1400)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:313)
at com.ibm.ws.naming.util.Helpers.processSerializedObjectForLookup(Helpers.java:884)
at com.ibm.ws.naming.jndicos.CNContextImpl.processResolveResults(CNContextImpl.java:1675)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1538)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1458)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:1130)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:122)
at javax.naming.InitialContext.lookup(InitialContext.java:363)
at oracle.toplink.jndi.JNDIConnector.connect(Unknown Source)
at oracle.toplink.sessions.DatabaseLogin.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.buildConnection(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.startUp(Unknown Source)
at oracle.toplink.threetier.ServerSession.connect(Unknown Source)
at oracle.toplink.publicinterface.DatabaseSession.login(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getSession(ToplinkServerSessionFactory.java:121)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getClientSession(ToplinkServerSessionFactory.java:56)
at com.deere.jdc.component.rcts.dao.RCTSGateway.findDealerASMByDealerNumber(RCTSGateway.java:194)
at com.deere.jdc.component.rcts.service.RCTSService.getRCTSDealerASM(RCTSService.java:159)
at com.deere.jdc.component.rcts.adapter.SoapRCTSService.getRCTSDealerASM(SoapRCTSService.java:107)
at java.lang.reflect.Method.invoke(Native Method)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.invokeServiceMethod(RCTSMessageHandler.java:103)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.processMessage(RCTSMessageHandler.java:62)
at com.deere.jdc.mqseries.MQJMSMessageHandler.onMessage(MQJMSMessageHandler.java:122)
at com.ibm.mq.jms.MQQueueReceiver.receiveAsync(MQQueueReceiver.java:861)
at com.ibm.mq.jms.SessionAsyncHelper.run(SessionAsyncHelper.java:401)
at java.lang.Thread.run(Thread.java:513)
[5/22/03 7:03:01:891 CDT] 2ec4c80 Helpers W NMSV0610I: A NamingException is being thrown from a javax.naming.Context implementation. Details follow:
Context implementation: com.ibm.ws.naming.jndicos.CNContextImpl
Context method: lookup
Context name: localhost/nodes/localhost/servers/server1
Target name: jdbc/DB239DataSource
Other data:
Exception stack trace: com.ibm.websphere.naming.CannotInstantiateObjectException: Exception occurred while the JNDI NamingManager was processing a javax.naming.Reference object. Root exception is java.lang.ClassCastException: org.apache.xerces.jaxp.SAXParserFactoryImpl
at javax.xml.parsers.SAXParserFactory.newInstance(Unknown Source)
at com.ibm.ejs.j2c.XMLReader.validate(XMLReader.java:673)
at com.ibm.ejs.j2c.XMLReader.init(XMLReader.java:241)
at com.ibm.ejs.j2c.XMLReader.<init>(XMLReader.java:165)
at com.ibm.ejs.j2c.ConnectorRuntime.getJ2C_Properties(ConnectorRuntime.java:341)
at com.ibm.ejs.j2c.poolmanager.PoolManager.<init>(PoolManager.java:541)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createCFD_Common(ConnectionFactoryBuilderImpl.java:699)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createConnectionFactoryDetails(ConnectionFactoryBuilderImpl.java:417)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.getObjectInstance(ConnectionFactoryBuilderImpl.java:1400)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:313)
at com.ibm.ws.naming.util.Helpers.processSerializedObjectForLookup(Helpers.java:884)
at com.ibm.ws.naming.jndicos.CNContextImpl.processResolveResults(CNContextImpl.java:1675)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1538)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1458)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:1130)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:122)
at javax.naming.InitialContext.lookup(InitialContext.java:363)
at oracle.toplink.jndi.JNDIConnector.connect(Unknown Source)
at oracle.toplink.sessions.DatabaseLogin.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.buildConnection(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.startUp(Unknown Source)
at oracle.toplink.threetier.ServerSession.connect(Unknown Source)
at oracle.toplink.publicinterface.DatabaseSession.login(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getSession(ToplinkServerSessionFactory.java:121)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getClientSession(ToplinkServerSessionFactory.java:56)
at com.deere.jdc.component.rcts.dao.RCTSGateway.findDealerASMByDealerNumber(RCTSGateway.java:194)
at com.deere.jdc.component.rcts.service.RCTSService.getRCTSDealerASM(RCTSService.java:159)
at com.deere.jdc.component.rcts.adapter.SoapRCTSService.getRCTSDealerASM(SoapRCTSService.java:107)
at java.lang.reflect.Method.invoke(Native Method)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.invokeServiceMethod(RCTSMessageHandler.java:103)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.processMessage(RCTSMessageHandler.java:62)
at com.deere.jdc.mqseries.MQJMSMessageHandler.onMessage(MQJMSMessageHandler.java:122)
at com.ibm.mq.jms.MQQueueReceiver.receiveAsync(MQQueueReceiver.java:861)
at com.ibm.mq.jms.SessionAsyncHelper.run(SessionAsyncHelper.java:401)
at java.lang.Thread.run(Thread.java:513)
2003.05.22 07:03:02.111--ServerSession(426724481)--Thread[asyncDelivery0,5,main]--EXCEPTION [TOPLINK-7060] (TopLink - 9.0.3 (Build 423)): oracle.toplink.exceptions.ValidationException
EXCEPTION DESCRIPTION: Cannot acquire data source [jdbc/DB239DataSource].
INTERNAL EXCEPTION: com.ibm.websphere.naming.CannotInstantiateObjectException: Exception occurred while the JNDI NamingManager was processing a javax.naming.Reference object. [Root exception is java.lang.ClassCastException: org.apache.xerces.jaxp.SAXParserFactoryImpl]LOCAL EXCEPTION STACK:
EXCEPTION [TOPLINK-7060] (TopLink - 9.0.3 (Build 423)): oracle.toplink.exceptions.ValidationException
EXCEPTION DESCRIPTION: Cannot acquire data source [jdbc/DB239DataSource].
INTERNAL EXCEPTION: com.ibm.websphere.naming.CannotInstantiateObjectException: Exception occurred while the JNDI NamingManager was processing a javax.naming.Reference object. [Root exception is java.lang.ClassCastException: org.apache.xerces.jaxp.SAXParserFactoryImpl]
at oracle.toplink.exceptions.ValidationException.cannotAcquireDataSource(Unknown Source)
at oracle.toplink.jndi.JNDIConnector.connect(Unknown Source)
at oracle.toplink.sessions.DatabaseLogin.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.buildConnection(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.startUp(Unknown Source)
at oracle.toplink.threetier.ServerSession.connect(Unknown Source)
at oracle.toplink.publicinterface.DatabaseSession.login(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getSession(ToplinkServerSessionFactory.java:121)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getClientSession(ToplinkServerSessionFactory.java:56)
at com.deere.jdc.component.rcts.dao.RCTSGateway.findDealerASMByDealerNumber(RCTSGateway.java:194)
at com.deere.jdc.component.rcts.service.RCTSService.getRCTSDealerASM(RCTSService.java:159)
at com.deere.jdc.component.rcts.adapter.SoapRCTSService.getRCTSDealerASM(SoapRCTSService.java:107)
at java.lang.reflect.Method.invoke(Native Method)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.invokeServiceMethod(RCTSMessageHandler.java:103)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.processMessage(RCTSMessageHandler.java:62)
at com.deere.jdc.mqseries.MQJMSMessageHandler.onMessage(MQJMSMessageHandler.java:122)
at com.ibm.mq.jms.MQQueueReceiver.receiveAsync(MQQueueReceiver.java:861)
at com.ibm.mq.jms.SessionAsyncHelper.run(SessionAsyncHelper.java:401)
at java.lang.Thread.run(Thread.java:513)
INTERNAL EXCEPTION STACK:
com.ibm.websphere.naming.CannotInstantiateObjectException: Exception occurred while the JNDI NamingManager was processing a javax.naming.Reference object. Root exception is java.lang.ClassCastException: org.apache.xerces.jaxp.SAXParserFactoryImpl
at javax.xml.parsers.SAXParserFactory.newInstance(Unknown Source)
at com.ibm.ejs.j2c.XMLReader.validate(XMLReader.java:673)
at com.ibm.ejs.j2c.XMLReader.init(XMLReader.java:241)
at com.ibm.ejs.j2c.XMLReader.<init>(XMLReader.java:165)
at com.ibm.ejs.j2c.ConnectorRuntime.getJ2C_Properties(ConnectorRuntime.java:341)
at com.ibm.ejs.j2c.poolmanager.PoolManager.<init>(PoolManager.java:541)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createCFD_Common(ConnectionFactoryBuilderImpl.java:699)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createConnectionFactoryDetails(ConnectionFactoryBuilderImpl.java:417)
at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.getObjectInstance(ConnectionFactoryBuilderImpl.java:1400)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:313)
at com.ibm.ws.naming.util.Helpers.processSerializedObjectForLookup(Helpers.java:884)
at com.ibm.ws.naming.jndicos.CNContextImpl.processResolveResults(CNContextImpl.java:1675)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1538)
at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1458)
at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:1130)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:122)
at javax.naming.InitialContext.lookup(InitialContext.java:363)
at oracle.toplink.jndi.JNDIConnector.connect(Unknown Source)
at oracle.toplink.sessions.DatabaseLogin.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.internal.databaseaccess.DatabaseAccessor.connect(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.buildConnection(Unknown Source)
at oracle.toplink.threetier.ConnectionPool.startUp(Unknown Source)
at oracle.toplink.threetier.ServerSession.connect(Unknown Source)
at oracle.toplink.publicinterface.DatabaseSession.login(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at oracle.toplink.tools.sessionmanagement.SessionManager.getSession(Unknown Source)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getSession(ToplinkServerSessionFactory.java:121)
at com.deere.jdc.toplink.ToplinkServerSessionFactory.getClientSession(ToplinkServerSessionFactory.java:56)
at com.deere.jdc.component.rcts.dao.RCTSGateway.findDealerASMByDealerNumber(RCTSGateway.java:194)
at com.deere.jdc.component.rcts.service.RCTSService.getRCTSDealerASM(RCTSService.java:159)
at com.deere.jdc.component.rcts.adapter.SoapRCTSService.getRCTSDealerASM(SoapRCTSService.java:107)
at java.lang.reflect.Method.invoke(Native Method)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.invokeServiceMethod(RCTSMessageHandler.java:103)
at com.deere.jdc.component.rcts.messaging.RCTSMessageHandler.processMessage(RCTSMessageHandler.java:62)
at com.deere.jdc.mqseries.MQJMSMessageHandler.onMessage(MQJMSMessageHandler.java:122)
at com.ibm.mq.jms.MQQueueReceiver.receiveAsync(MQQueueReceiver.java:861)
at com.ibm.mq.jms.SessionAsyncHelper.run(SessionAsyncHelper.java:401)
at java.lang.Thread.run(Thread.java:513)This looks like a classPath issue.check ur runtime
class path and make sure xerces is before any other
parser(xml4j /oracleParser)
vasanth -
NamingException and ClassCastException during jndi lookup
Hi,
I am trying to access the SAP database(MaxDb)using JNDI lookup in WebDynpro simple java project.
But, during lookup I am getting the following error
com.sap.engine.services.jndi.persistent.exceptions.NamingException: Exception during lookup operation of object with name : jdbc/VSLOOKUP , can not resolve object reference [Root exception is java.lang.ClassCastException]
at com.sap.engine.services.jndi.implclient.ClientContext.lookup(ClientContext.java:469)
at com.sap.engine.services.jndi.implclient.ClientContext.lookup(ClientContext.java:558)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at com.vitalspring.healthbenefits.dbaccess.DBAccessHelper.main(DBAccessHelper.java:51)
Caused by: java.lang.ClassCastException
at com.sap.engine.services.connector.ResourceObjectFactory.getObjectInstance(ResourceObjectFactory.java:149)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:301)
at com.sap.engine.services.jndi.implclient.ClientContext.lookup(ClientContext.java:466)
... 3 more
Can anybody throw some light on it?
Because, it's urgent for my project.
I would appreciate any kind of help in this regard.
Thanks & Regards,
Rambabu KancharlaHello Anilkumar,
Sorry for delay in reply as I was on out of work and didn't get chance to check the SDN forums.
I was able to do it in latest version sneak preview 11.
I think the problem with the version.
Thanks,
Rambabu -
Servlets and Workspaces (AND ClassCastExceptions)
I have a similiar situation but am not getting a ClassCastException but rather a
NullPointerException when I try
to use a contained object in my stored object.
I do this in the server:
WorkspaceServicesDef workspaceServices = services.workspace();
WorkspaceDef defaultWS = workspaceServices.getWorkspace();
WorkspaceDef dataWS = defaultWS.getWorkspace("DATA_WORKSPACE",
WorkspaceDef.CREATE,
WorkspaceDef.SCOPE_SERVER);
dataWS.store(cMSKey, ms);
and this in the JSP page:
// Get the default T3Client Workspace
WorkspaceDef defaultWS = t3.services.workspace().getWorkspace();
// Attach to the system subWorkspace already created
WorkspaceDef myDataWS = defaultWS.getWorkspace("DATA_WORKSPACE",
WorkspaceDef.ATTACH,
WorkspaceDef.SCOPE_SERVER);
MimicServer ms = (MimicServer) myDataWS.fetch("MimicServer");
Questions:
1)Must I make the Object I want to store in the WorkSpace Serializable?? The
Documentation says it can just be
a Java Object??
2) In my JSP page I get a good Object reference, but its contents are null
(Probably because I didn't implement write() and read()).
Thanks,
matt obrien
[email protected]
Mark Griffith wrote:
> Alexandre:
>
> Although byte for byte the FooObject is the same, according to the VM they
> are different class. This is because a class's type distinctiveness is
> based not only its interfaces,methods,data members etc but ALSO on its
> classloader. There is a different classloader for the
> serversclasses_FooObject and the servletclasses_FooObject so they are
> considered different, so you get a CCE.
>
> Problem is that the servletclasses directory is designed to solve the
> problem of reloading servlet classes and objects they used when modified
> without having to restart the server. But this introduces the problem
> discussed above. The only solution currently is to put the FooObject only in
> one place or another. Unfortunately if you want a class in servletclasses
> to interact classes loaded by the system/server classloader you cannot put
> the class in the servletclasses without getting a CCE on reload of the
> servlet in servletclasses. In this case you must put the class only in the
> serverclasses directory. And this means of course you can only reload the
> FooObject in the serverclasses directory if you bounce the server. (the
> default java classloading behavior).
>
> This will be handled more elegantly in our next major release Spring 2000,
> however note (and prepare by designing accordingly) the solution is to allow
> the Servlet to interact with interfaces not actual classes and these
> interfaces can be stuffed into system/server loaded classes such as
> workspaces and httpsessions, thus preventing CCE's.
>
> Cheers
> Mark G
>
> Alexandre Aubry wrote:
>
> > I have a named workspace which is created at startup of the server
> > within a starup class.
> > When the startup method is called, I instantiate and put an object
> > called FooObject in the workspace.
> >
> > Then, in a servlet, called Dispatcher, in the service method, I
> > retrieved my named workspace and the object
> > FooObject which is in this workspace. At this time, I have a
> > ClassCastException.
> >
> > To give you more details about my configuration, here is my directory
> > configuration:
> > weblogic
> > |----------- serverclasses
> > |------------- startupObject
> > |------------- FooObject
> > |----------- servletclasses
> > |------------- myServlet
> > |------------- FooObject (which is exactly
> > the same of the serverclasses one)
> >
> > Do you have any ideas ? The idea is to instantiate a given object at the
> > startup of the web server and to retrieve
> > these objects within servlets and JSPs.
> >
> > Regards.
> >
> > --
> > Alexandre Aubry
> > Consultant
> > Fi System, The Web Agency
> > http://www.fisystem.fr
> > mailto:[email protected]
> > Phone: +33 1 55 04 03 03 Fax: +33 1 55 04 03 04
>
> --
> =====================================================
> Reply to the newsgroup. Don't reply to this mail
> alias. This is used only for answering posts on
> WebLogic Newsgroups.
> =====================================================
-
Serialization and ClassCastException
hi all,
I have a a class like this:
import java.io.Serializable;
import java.util.ArrayList;
public class Fact implements Serializable {
private String name;
private ArrayList al=new ArrayList();
public Fact(String _name) {
name=_name;
al.add("Pippo");
al.add("Pluto");
al.add("Papero");
public String getName() {
return name;
public ArrayList getList() {
return al;
and I want to serialize objects of it in a mySQL database table. the table has two field: 'name', a varchar(20) used as a key and 'object', a blob field to store Java objects.
this is my code to put object in the table (it seems to work fine):
public boolean putFact(Fact fact) { // inserts a new fact in the db
Fact result=null;
try {
PreparedStatement pstmt=conn.prepareStatement("INSERT INTO facts VALUES (?, ?)");
pstmt.setString(1, fact.getName());
pstmt.setObject(2, fact);
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException ex) {
System.out.println(ex.toString());
return false;
return true;
this code musts get an object from the table:
public Fact getFact(String factName) {
Fact result=null;
try {
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT object FROM facts WHERE name='"+factName+"'");
while (rs.next()) {
result=(Fact)rs.getObject(1);
rs.close();
stmt.close();
} catch (SQLException ex) {
System.out.println(ex.toString());
return null;
return result;
on the 7th line of the getFact method I have a ClassCastException: why?
in addition, if I remove the ArrayList element from the Fact class, my code works fine.
any help?
thanks a lot. :)first of all, thanks for your support, sylviae. :)
well, I used your syntax
(System.err.println(anObject.getClass());) and the
result is "class Fact": so, the serialization process
seems to be right.
then, I tried to change the field from BLOB to
MEDIUMBLOB type, and now it works! so, I suppose that
the BLOB type was not able to manage a Fact object.That doesn't really make much sense. If in the revised code you were getting the exception on the line that casts anObject to be a Fact object, then I'd start to wonder about ClassLoader issues. Still, given that you've got it working, you're probably not concerrned about the exact cause.
>
in the end, can you suggest me a way to generate SQL
statements, if mine has the error you said?Use the prepareStatement mechanism as you did for the insert statement, and associate the value with the "?" parameter. I know this looks more clumsy, and is certainly more code, but it is much safer. It's also capable of a more effecient execution because the underlying implementation can note that it's already parsed the statement and avoid having to do it again.
Sylvia. -
XMLDocument and ClassCastException
We recently upgraded to Java 1.4.1_02 from Java 1.3.0.
The following type casting worked in java 1.3.0, but throws java.lang.ClassCastException while running in Java 1.4.1.
XmlDocument xdoc = (XmlDocument)doc;
where 'doc' is a variable of interface org.w3c.dom.Document
Do anybody have an idea why it does not work in Java 1.4.1?
Does the parser.jar file (that has the classfiles XMLDocument and Document) need to be upgraded for Java 1.4.1?The problem was because Java 1.4.1 comes with its apache's implementation of XMLDocument and this was interfering with the XMLDocument class in parser.jar, which was sun's implementation.
So, when we removed parser.jar from classpath, it is working fine. -
Hello
There is a very useful functionality, that I think should be implemented in
TreeSet nad HashSet
that is the method: Object get(Object o).
The method should return the same object from colletion as the parameter object.
In TreeSet complexity would be log(n), in HashSet would be constant.
With lack of this functionality one must implement collections on maps, so the unnecessary and more complex type will be used.
What do you think about this?
Regards
MarcinThis is a strange question :)
I assume, the OP does not want the object to be returned, but the one from the Set, which is equals to the given object, or null. This might be useful, if operating in a template scenario like JINI, where the given object defines a template (or pattern) that suffices the equals method but does not have further data contained by the object in the Set (which actually should be solved by using a Map). Or if you want to match and return the equal object to have the exact instance. E.g. for a simple Set:public Object get(Object o) {
for (Object object : objects) {
if (object.equals(o)) {
return object;
}which might be optimized for HashSet (quicker due to buckets) and TreeSet (quicker due to tree structure).
�dit: Ok, my first scenario was the one. I'd suggest using a Map instead, which is much cleaner wrt. equals/hashcode implementation and contract.
Message was edited by: stefan.schulz -
TreeSet and Comparator problem
Hi all ,
I am using jdk1.3 , jbuilder4.0 , Apache Tomcat 1.3.14 and Window 98 and Access Database.
I have comparator code below which give me some problem
public int compare(Object x, Object y) {
CompanyJB a = (CompanyJB)x , b = (CompanyJB)y;
double aMinratio = a.getMinratio();
double bMinratio = b.getMinratio();
int aCompid = a.getCompanyid();
int bCompid = b.getCompanyid();
String aRankstr = a.getRank();
String bRankstr = b.getRank();
int tmp;
tmp = new Integer(aCompid).compareTo(new Integer(bCompid));
if (tmp == 0) {
return tmp;
tmp = a.getRank().compareTo(b.getRank());
if (tmp !=0) {
return tmp;
tmp = new Double(a.getMinratio()).compareTo(new Double(b.getMinratio()));
if (tmp !=0) {
return tmp;
return new Double(a.getMinratio()).compareTo(new Double(b.getMinratio()));
Client source code below :
TreeSet list2 = new TreeSet(new test());
//list2.add(new test(2,1.9,4.2,"NA")); // 1
list2.add(new test(11,8.5,8.5,"2")); // 3
list2.add(new test(1,0.6,0.6,"NA")); // 2
list2.add(new test(11,2.5,2.3,"NA")); // 4
list2.add(new test(12,0.3,0.3,"1")); // 5
list2.add(new test(2,1.9,1.2,"NA"));
list2.add(new test(12,0.9,0.9,"NA"));
Iterator iterator2 = list2.iterator();
while(iterator2.hasNext()){
System.out.println(((test)iterator2.next()).getCompanyid());
My problem is that the print out display duplicate company id "12" in the set.
It should not display the duplicate company id in the set.
What is the problem here ?? Anybody have ideas ??Actually, a Set, as defined in java.util.Set, should not contain duplicate members. With SortedSets, however, there must also be consistency between equals() and the compareTo() (or Comparator.compare()). In other words, equal objects must sort to the same or an adjacent place in order for the Set semantics to work.
In your example, "equal" objects can appear in completely different positions in the sort order, throwing off this consistency. When adding, it's traversing the tree in Rank,MinRatio order. If by chance it runs across a node that has the same company id, it stops and leaves the trees alone (as it does with id=11). If you try graphing the tree out by hand using your compare() as left/right criteria (negative left, positive right, 0 don't change), it'll become immediately clear what's happening.
Also of interest: you'll find that you can't add test(34,1.9,1.2,"NA") to the list: because it's Rank and MinOrder is the same as the id=2 entry, it'll never get added. -
Problem with TreeSet and tailset
I have a TreeSet with a large number of objects (7000+). I am using tailset to get the specific objects I want from the TreeSet. This is usually only the last few objects. Then, I write this returned set to an ObjectOutputStream using the writeObject method. The problem is that it takes a long time for the writeObject method to complete. If my original TreeSet only has around 1000 objects in it, then the writeObject method completes rather quickly. It seems as though the TreeSet returned by the tailSet method is much larger than I think it is. Even if tailSet returns a TreeSet with no objects in it, the writeObject still takes the same (long) amount of time to complete.
Is there something I do not understand about the TreeSet returned by the tailSet method?
Thanks,
Jeff Lueders
[email protected]tailSet() just creates an view from the original Set. In the case of TreeSet, which is implemented via TreeMap, not much happens when you create this view. Only a tiny Object is created, of type private class TreeMap.SubMap, which holds information of the first element of this view and that it is supposed to extend all the way to the end. So every access to the tail set still needs to go through your huge original Set.
One way to reduce this lookup overhead to a single-time operation would be to copy the tail set into a new TreeSet, which is definitely truly smaller but will not be directly backed by the original Set anymore. So concurrent changes inside any of the Sets will not effect the other.
Set tailSet = new TreeSet(originalSet.tailSet(startKey)); -
I'm trying to exchange messages between two applications running on Oracle 9iAS (9.0.3) and I'm getting a ClassCastException.
Basically, I'm sending Command instances over the wire, which are to be executed on the other end. The concrete Command class is in a jar, which is in both web applications' WEB-INF/lib. I checked that both apps contain the same version of the jar. I have a simple JSP in both apps to publish a message containing a command. For testing purposes, both apps run in the same JVM (OC4J).
Both apps have a TopicSubscriber instance connected to the same Topic. When I publish an instance of ObjectMessage with the Command instance as the payload, both apps receive the message, but upon getting the payload (message.getObject()) the app that did not send the message fails with a ClassCastException.
Here's an example:
App A publishes an ObjectMessage with an instance of com.foo.FooCommand on the Topic
App A receives the message and successfully gets the FooCommand from the received message
App B receives the message and fails with a ClassCastException when getting the FooCommand from the message.
The ClassCastException looks like this:
java.lang.ClassCastException: com.foo.FooCommand
Publisher:
Message m = topicSession.createObjectMessage(new FooCommand());
Subscriber:
ObjectMessage om = (ObjectMessage) message;
Command c = (Command) om.getObject();
The second line in the subscriber code fails. Command is the interface that has to be implemented by all commands, it extends Serializable.
I already tried adding a serialVersionUID to the command class, but it didn't help. Is there any other way into making the ClassLoader believe that both instances are the same?
Any ideas?
TIA,
NilsNils, the ClassCastException is expected if you put your class in both applications' WEB-INF/lib. The class is loaded by App A's web module classloader and, separately, loaded by App B's web module classloader. You have two instances of Class that have the same underlying bits of class format but loaded by two different classloaders, which is the classic situation that leads to ClassCastException.
The guideline of the solution: let a common parent classloader of those two classloaders load the class. What you need to do is to make your jar visible to all applications or loaded by a parent application of your App A and App B. The way to do it is mentioned in my message
Re: Quick question on parent-child relationship in OC4J 10.1.3
The simplest of all is to put your jar in
<ORACLE_HOME>/j2ee/home/applib.
Hope this help you. -
ServerAffinity and Stateless EJB and ClassCastException
Hello,
We are using Weblogic Server 8.1 SP4.
I have an EJB set the bean-load-algorithm to weight-based-affinity because we have a client cluster communicating with a server cluster and would like to maintain affinity with a single node of the server cluster.
We are getting the following exception when making a call from the client to the server:
Caused by: java.lang.ClassCastException
at weblogic.rmi.cluster.WeightBasedReplicaHandler.chooseReplica(WeightBa
sedReplicaHandler.java:53)
at weblogic.rmi.cluster.BasicReplicaHandler.chooseReplicaUsingAffinity(B
asicReplicaHandler.java:567)
at weblogic.rmi.cluster.BasicReplicaHandler.loadBalance(BasicReplicaHand
ler.java:283)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteR
ef.java:241)
at org.collegeboard.ecommerce.content.ejb.AdService_5ucrg0_EOImpl_814_WL
Stub.retrieveAd(Unknown Source)
... 25 more
When I switch the server EJB back to the default of round robin, it works fine.
The default cluster algo for both clusters are the default (at the server level).
Any ideas?
Thanks in advance.
--baharI think you should contact [email protected] This looks like a bug to me.
-- Rob
WLS Blog http://dev2dev.bea.com/blog/rwoollen/ -
Generics and ClassCastException
Does anyone know why in the following code map.get("one").getClass() is the only line which throws ClassCastException.
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 1);
System.out.println("map.get(1) =" + map.get(1));
Object obj = map;
Map<String, Number> map2 = (Map<String, Number>) obj;
map2.put("one", 1L);
System.out.println("map2.get(1) =" + map2.get(1));
System.out.println("map2.get(one) =" + map2.get("one"));
Map<String, Integer> map3 = (Map<String, Integer>) obj;
System.out.println("map3.get(1) =" + map3.get(1));
// Doesn't throw ClassCastException even though the return value is a Long, not Integer.
Object objOne = map3.get("one");
System.out.println("map3.get(one) =" + map3.get("one") + ", " + objOne.getClass());
try {
// throws ClassCastException ????.
System.out.println("map3.get(one).getClass() =" + map3.get("one").getClass());
} catch (ClassCastException e) {
System.out.println("map3.get(one).getClass() throws " + e);
}I have JDK 1.5.0_02Map<String, Integer> map3 = (Map<String,<String, Integer>) obj;
System.out.println("map3.get(1) =" +
(1) =" + map3.get(1));
// Doesn't throw ClassCastException even
ion even though the return value is a Long, not
Integer.
Object objOne = map3.get("one");
System.out.println("map3.get(one) =" +
ne) =" + map3.get("one") + ", " +
objOne.getClass());
try {
// throws ClassCastException ????.
System.out.println("map3.get(one).getClass()
etClass() =" + map3.get("one").getClass());
} catch (ClassCastException e) {
System.out.println("map3.get(one).getClass()
etClass() throws " + e);
I have JDK 1.5.0_02
Okay, now that I have my X-Ray Specs on, I see the longs. At first, I was surprised
by the class cast, but what I think is happening is that the expression:
map3.get("one")Has an implicit cast in it, thanks to generics:
((Integer)map3.get("one"))And it's that cast that's generating the runtime exception. Now, how do folks
dump out class files in readable format? I'd like to see to myself...
Maybe you are looking for
-
How do I edit a sign on a truck out of my picture in Aperture?
Hello, I'm new to Aperture and I've been reading the manuals, doing youtube videos, anything I can get my hands on. I'm trying to figure out how to get rid of the Budweiser sign on the truck in one of my pictures. I want to take it out because I'm pu
-
HT1918 How can I delete an iTunes account?
Need to delete new account in order to use current email on the original account
-
Is it possible to get an apple ID without having to put a way of payment?
Well not everyine has a visa card or any other kind of card and since there are free apps in the app store it seems natural that we get the ability to creat an apple ID and download free games without having to have a visa card (since it is useless f
-
Require fallowing files due to corrupt download
My download got corrupt because of some network issues/power failure, so require below files. windows_ia32_jrockit_160_05_R27.6.2-20_jrockit.zip ( from Oracle WebLogic Server 11gR1 (10.3.1) + OEPE - Package Installer link http://download.oracle.com/o
-
FreeNAS 9.3 with HP M6412-A Fibre Channel Drive Enclosures
Hi,I have acquired two HP M6412-A Fibre Channel Drive Enclosures with 14 dual-port SAS15K disk drives (7 per enclosure) in a recent company takeover.I do not have any HP software licenses for the HP EVA4400 Dual Controller Array that came with the di