javax.rmi.PortableRemoteObject.narrow returning null


EJB programming & troubleshooting: javax.rmi.PortableRemoteObject.narrow returning null

  1. javax.rmi.PortableRemoteObject.narrow returning null (6 messages)

    Hi, I am new to EJB and this is the first client I am trying. I wrote a "Hello world" SessionBean and deployed it into Sun Java System Application Server Platform Edition 9.0. The EJB module was deployed successfully (can see it on the console). Then I wrote a client to connect to the ejb component. Following is the client code package client; import java.util.*; import javax.naming.*; import java.rmi.*; import javax.rmi.*; import javax.ejb.*; import test.*; public class AdviceClient { /** Creates a new instance of AdviceClient */ public AdviceClient() { } public static void main(String[] args) { try { Properties p = new Properties(); p.put("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory"); p.put("java.naming.provider.url", "iiop://"); Context ic = new InitialContext(p); Hashtable ht = ic.getEnvironment(); Iterator enu = ht.keySet().iterator(); while (enu.hasNext()) { System.out.println(; } Object o = ic.lookup("ejb/AdviceBean"); if (o == null) { System.out.println("o is null"); } else { System.out.println("o is NOT null"); } AdviceRemoteHome home = (AdviceRemoteHome) javax.rmi.PortableRemoteObject.narrow( o, AdviceRemoteHome.class); //<--ISSUE HERE if (home == null) { System.out.println("home is null"); } else { System.out.println("home is NOT null"); } //AdviceRemoteBusiness advice = home.create(); //System.out.println("advice is " + advice.getAdvice()); } catch (ClassCastException e) { System.out.println("classcasteexception"); e.printStackTrace(); } catch (Exception e) { System.out.println("exception occured"); e.printStackTrace(); } } } The issue is when I call the narrow method, it returns a null object and there is no ClassCastException. I would like to know what could be wrong prompting a null object getting returned. TIA, Babu
  2. Sorry. Missed to include the output i got. Output: java.naming.provider.url java.naming.factory.initial java.naming.factory.url.pkgs java.naming.factory.state o is NOT null home is null
  3. Simply print o like System.out.println(o); and see what you get. cheers,
  4. System.out.println(o) gives the following: IOR:000000000000002b524d493a746573742e.......06066781020101010000000f Thanks, Babu
  5. Let us go step by step: 1: first look at the code of the narrow method of PortableRemoteObject Class. public java.lang.Object narrow ( java.lang.Object narrowFrom, java.lang.Class narrowTo) throws ClassCastException { java.lang.Object result = null; if (narrowFrom == null) { return null; } if (narrowTo == null) { throw new NullPointerException("invalid argument"); } Class narrowFromClass = narrowFrom.getClass(); try { // Will a cast work? if (narrowTo.isAssignableFrom(narrowFromClass)) { // Yep, so we're done... result = narrowFrom; } else { // No. Is narrowTo an interface that might be // implemented by a servant running on iiop? if (narrowTo.isInterface() && // What was this test supposed to achieve? // narrowTo != java.rmi.Remote.class && narrowTo != && narrowTo != { // Yes. Ok, so assume the current stub (narrowFrom) is an // ObjectImpl (it should be a _"xxx"_Stub). If it is not, // we'll catch it below and end up failing with a // ClassCastException... org.omg.CORBA.portable.ObjectImpl narrowObj = (org.omg.CORBA.portable.ObjectImpl) narrowFrom; // Create an id from the narrowTo type... String id = RepositoryId.createForAnyType(narrowTo); // Can the server act as the narrowTo type? if (narrowObj._is_a(id)) { // Yes, so try to load a stub for it... result = Utility.loadStub(narrowObj,narrowTo); } } } } catch(Exception error) { result = null; } if (result == null) { throw new ClassCastException(); } return result; } 2: Use reflection on the object o and print out its super class and interfaces. 3: Go to the server and check what jndi name your bean has,if your accessing remotely then what is the jndi name by which you must look up-- I am not sure ejb/something is the jndi name. 4: Since you comfirmed that the object o is not null, try casting it some other class and see if you get ClassCastException Do these things, you will solve the problem yourself cheers,
  6. Hi, I had also suffered from this problem, but i succeed to remove this problem you used the below code for setting the property ****************************************** Properties p = new Properties(); p.put("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory"); p.put("java.naming.provider.url", "iiop://"); ******************************************* just change this code to below, ****************************************** Properties p = new Properties(); p.put("org.omg.CORBA.ORBInitialHost","localhost"); p.put("org.omg.CORBA.ORBInitialPort","3700"); ******************************************* I m using Netbeans 5.5/Sun application server 9 Enjoy the code From Sohail Khalid Innovative Pvt. Ltd Lahore Pakistan [email protected]
  7. You can find a simple implementation example at :

    Reading it will fix your problem!