Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4420864

Null Pointer Exception When Creating javax.naming.InitialContext

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • deploy
    • x86
    • windows_nt



      Name: boT120536 Date: 03/01/2001


      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      The java bean I try to access from Visual Basic using the bridge trys to
      connect across the network to an EJB App Server.

      The beans methods get invoked OK in order to define the context factory, url,
      user and password for connection attempt but gives a NullPointerException when
      I create the InitialContext.

      The same code works exactly as expected when invoked from within a Java IDE
      (JBuilder) or from a DOS prompt.

      There now follows:
            - source code for the Java bean
            - stack trace of exception
            - class paths is use

      The source code for the bean is as follows:


      package xxx.beans;


      import java.io.Serializable;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      import java.io.IOException;
      import java.io.PrintWriter;
      import java.io.StringWriter;
      import java.rmi.RemoteException;
      import javax.ejb.CreateException;
      import javax.ejb.RemoveException;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import java.util.Properties;
      import java.security.ProtectionDomain;
      import java.security.CodeSource;
      import java.net.URL;
      import java.util.Iterator;
      import xxx.ejbs.ReqReply;
      import xxx.ejbs.ReqReplyHome;


      public class ReqReplyBean implements Serializable {

        private ReqReplyHome home = null;
        private String jndiName;
        private String url;
        private String user;
        private String password;
        private Properties props;
        private String error = "";

        public ReqReplyBean() {
        }

        void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
          ois.defaultReadObject();
        }

        void writeObject(ObjectOutputStream oos) throws IOException {
          oos.defaultWriteObject();
        }

        public String processMsg(String msg) {
          try {
            ReqReplyHome home = getHome();
            ReqReply reqreply = home.create();
            String result = reqreply.processMsg(msg);
            reqreply.remove();
            return result;
          } catch(Exception ex) {
            logEx(ex);
            return "";
          }
        }

        private ReqReplyHome getHome() throws ClassNotFoundException,
      NamingException, RemoteException, IOException {
          if(home == null) {
            if(jndiName == null)
              throw new NamingException("missing JndiName setting");
            Context ctx = getInitialContext();
            home = (ReqReplyHome) ctx.lookup(jndiName);
          }
          return home;
        }

        private Context getInitialContext() throws NamingException {
          if(url == null)
            throw new NamingException("missing url setting");
          if(user == null)
            throw new NamingException("missing user setting");
          if(password == null)
            throw new NamingException("missing password setting");
          props = getProperties(url, user, password);
          if(props == null)
            throw new NamingException("null properties");
          // *** The following line causes the null pointer exception ***
          return new InitialContext(props);
        }

        private Properties getProperties(String url, String user, String password) {
          if(password == null)
            password = "";

          Properties p = new Properties();

          p.put (Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
          p.put(Context.PROVIDER_URL, url);
          p.put(Context.SECURITY_PRINCIPAL, user);
          p.put(Context.SECURITY_CREDENTIALS, password);

          return p;
        }

        private void logEx(Throwable ex) {
          StringWriter sw = new StringWriter();
          PrintWriter pw = new PrintWriter(sw);
          pw.println("unexpected exception - ex = " + ex);
          ex.printStackTrace(pw);
          Properties props = System.getProperties();
          pw.println("java.class.path = " + props.getProperty("java.class.path"));
          pw.println("sun.boot.class.path = " + props.getProperty ("sun.boot.class.path"));
          pw.flush();
          error = sw.toString();
        }

        public void setJndiName(String newJndiName) {
          jndiName = newJndiName;
        }

        public String getJndiName() {
          return jndiName;
        }

        public void setUrl(String newUrl) {
          url = newUrl;
        }

        public String getUrl() {
          return url;
        }

        public void setUser(String newUser) {
          user = newUser;
        }

        public String getUser() {
          return user;
        }

        public void setPassword(String newPassword) {
          password = newPassword;
        }

        public String getPassword() {
          return password;
        }

        public String getError() {
          String tmp = getClassInfo() + "\n" + getPropsInfo() + "\n" + error;
          error = "";
          return tmp;
        }

        private String getPropsInfo() {
          String str = "";
          if(props == null)
            return str;
          Iterator iter = props.keySet().iterator();
          while(iter.hasNext()) {
            Object key = iter.next();
            Object value = props.get(key);
            str += "\n" + key + " = " + value;
          }
          return str;
        }

        private String getClassInfo() {
          Class klass = javax.naming.InitialContext.class;
          Package pkg = klass.getPackage();
          String tmp = "CodeSource:\ngetImplementationVendor = " + pkg.getImplementationVendor();
          tmp += "\ngetImplementationVersion = " + pkg.getImplementationVersion();
          tmp += "\ngetSpecificationVendor = " + pkg.getSpecificationVendor();
          tmp += "\ngetSpecificationVersion = " + pkg.getSpecificationVersion();
          ProtectionDomain pd = klass.getProtectionDomain();
          CodeSource cs = pd.getCodeSource();
          if(cs != null) {
            URL url = cs.getLocation();
            tmp += "\nCodeSource = " + url;
          }
          return tmp;
        }
      }

      A dump of the Hashtable of properties passed to the InitialContext constructor
      is as follows:

      java.naming.provider.url = t3://xxx.com:9988
      java.naming.factory.initial = weblogic.jndi.WLInitialContextFactory
      java.naming.security.principal = abell
      java.naming.security.credentials = xxx.yyy

      The stack trace for the exception is as follows:

      unexpected exception - ex = java.lang.NullPointerException
      java.lang.NullPointerException
      at com.sun.naming.internal.ResourceManager.getInitialEnvironment (ResourceManager.java:139)
      at javax.naming.InitialContext.init(InitialContext.java:214)
      at javax.naming.InitialContext.<init>(InitialContext.java:194)
      at xxx.beans.ReqReplyBean.getInitialContext(ReqReplyBean.java:81)
      at xxx.beans.ReqReplyBean.getHome(ReqReplyBean.java:65)
      at xxx.beans.ReqReplyBean.processMsg(ReqReplyBean.java:50)

      java.class.path = C:\JBuilder35\jars\jConnect-5.2
      \classes\jconn2.jar;C:\JBuilder35\jars\EjbAppServer.jar

      sun.boot.class.path = C:\jdk1.3\jre\lib\rt.jar;C:\jdk1.3
      \jre\lib\i18n.jar;C:\jdk1.3\jre\lib\sunrsasign.jar;C:\jdk1.3
      \jre\classes;C:\jdk1.3\jre\lib\plugprov.jar;C:\jdk1.3\lib\tools.jar;C:\jdk1.3
      \classes;C:\jdk1.3\jre\lib\jaws.jar
      (Review ID: 113071)
      ======================================================================

            jmelvin James Melvin (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: