Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Struts + EJB = "java.lang.ClassCastException" !!!

Options
  • 29-12-2004 5:17pm
    #1
    Registered Users Posts: 194 ✭✭


    I have tried everything i know but still get a ClassCastException. Am I using the PortableRemoteObject.narrow(); correctly??
    public ActionForward execute(
    			ActionMapping mapping,
    			ActionForm form,
    			HttpServletRequest request,
    			HttpServletResponse response)
    			throws Exception {
    			AddartistForm addartistForm = (AddartistForm) form;
    		
    			String artistID = addartistForm.getArtistID();
    			String artistName = addartistForm.getArtistName();
    			String description = addartistForm.getDescription();
    			String imageFileID = addartistForm.getImageFileID();
    			request.setAttribute("artistID", artistID);
    			request.setAttribute("artistName", artistName);
    			request.setAttribute("description", description);
    			request.setAttribute("imageFileID", imageFileID);
    			
    			Hashtable props = new Hashtable();
    
    			props.put(
    			InitialContext.INITIAL_CONTEXT_FACTORY,
    			"org.jnp.interfaces.NamingContextFactory");
    			props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
    
    			InitialContext initialContext = new InitialContext(props);	
    				
    			Object obj = initialContext.lookup("ie.dit.ft354.group4.cmp.ArtistHome.JNDI_NAME");
    			ie.dit.ft354.group4.cmp.ArtistHome artistHome = (ie.dit.ft354.group4.cmp.ArtistHome) PortableRemoteObject.narrow(obj, ie.dit.ft354.group4.cmp.ArtistHome.class);
    			try {
    			ie.dit.ft354.group4.cmp.Artist myBean = artistHome.create(artistID, artistName, description, imageFileID);
    					} catch (RemoteException e) {
    						e.printStackTrace();
    					} catch (CreateException e) {
    						e.printStackTrace();
    					} catch (ClassCastException e) {
    						e.printStackTrace();
    					}	
    
    					// Forward control to the specified success target
    
    					return (mapping.findForward("artistAdded"));
    				}
    


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Where do you get the class cast exception?

    My guess is here...

    ie.dit.ft354.group4.cmp.Artist myBean = artistHome.create(artistID, artistName, description, imageFileID);

    Shouldn't it be...

    ie.dit.ft354.group4.cmp.Artist myBean = (ie.dit.ft354.group4.cmp.Artist) artistHome.create(artistID, artistName, description, imageFileID);


  • Registered Users Posts: 194 ✭✭pbarry


    Im getting the exception at:

    ie.dit.ft354.group4.cmp.ArtistHome artistHome = (ie.dit.ft354.group4.cmp.ArtistHome) PortableRemoteObject.narrow(obj, ie.dit.ft354.group4.cmp.ArtistHome.class);

    I've hit a brick wall.............!!
    I cant think of any alternative to casting the lookup() result?
    java.lang.ClassCastException
    	at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:293)
    	at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)
    	at ie.dit.ft354.group4.struts.action.AddartistAction.execute(AddartistAction.java:76)
    


  • Registered Users Posts: 610 ✭✭✭nialo


    The reference for the object lookup may be wrong. Make sure that u are using the correct reference. This would cause a class cast exception if you are returning a different object!
    Object obj = initialContext.lookup("????");
    


  • Registered Users Posts: 194 ✭✭pbarry


    Cheers nialo,

    Your right, I'm not returning the correct lookup(). My server gives me the following warning when i initially run it:
    Depends On Me: , ObjectName: jboss.j2ee:module=MyStoreMgr.jar,service=EjbModule state: FAILEDI Depend On:  Depends On Me: javax.management.InstanceAlreadyExistsException: jboss.j2ee:jndiName=ArtistBean,service=EJB already registered.]
    

    How can I correct this?

    I used System.out.println(obj.getClass().getName()); after the lookup()
    call and got "$Proxy51"

    Something wrong big time!!


  • Registered Users Posts: 1,994 ✭✭✭lynchie


    pbarry wrote:
    Cheers nialo,

    Your right, I'm not returning the correct lookup(). My server gives me the following warning when i initially run it:
    Depends On Me: , ObjectName: jboss.j2ee:module=MyStoreMgr.jar,service=EjbModule state: FAILEDI Depend On:  Depends On Me: javax.management.InstanceAlreadyExistsException: jboss.j2ee:jndiName=ArtistBean,service=EJB already registered.]
    

    How can I correct this?
    I bet you have two beans both with the same JNDI name? check your ejb-jar.xml and jboss.xml.
    pbarry wrote:
    I used System.out.println(obj.getClass().getName()); after the lookup()
    call and got "$Proxy51"

    Something wrong big time!!

    Haven't verified this but you may be able to to the following to check the class returned. Narrow the class to an Object i.e. use PortableRemoteObject.narrow(obj,Object.class)

    This should narrow the object from the JBoss proxy object to an object instance. You should then be able to class getClass().getName() to figure out it's runtime instance type


  • Advertisement
  • Registered Users Posts: 194 ✭✭pbarry


    I bet you have two beans both with the same JNDI name? check your ejb-jar.xml and jboss.xml.

    Cheers lynchie, I had two ArtistBeans, thats what the problem was!!

    nice1 everyone...........


Advertisement