File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Using the Singleton pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using the Singleton pattern" Watch "Using the Singleton pattern" New topic

Using the Singleton pattern

Kamal Patel

Joined: Nov 05, 2003
Posts: 18
I am creating a Singleton class, so that only one instance of a object can ever be created. I am however getting an error with my code where I call the below command which is in my code below:
'instance = new ldapConnection(userID, pwd);' AND 'return ctx;'
I am not sure why it is complaining about the code - I am using an IDE and am getting a red line under these two lines of code meaning there is something wrong with it! Is it because I have not initialised my variables or something???

I have given my code below.
public class ldapConnection

/** Creates a new instance of ldapConnection */
private static DirContext instance = null;
public static DirContext getInstance(String userID, String pwd)
if (instance == null){
instance = new ldapConnection(userID, pwd);}
return instance;
protected DirContext ldapConnection(String ID, String password){

Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid"+ID+", ou=Directory Administrators, dc=db, dc=com");
env.put(Context.SECURITY_CREDENTIALS , password);

try {
DirContext ctx = new InitialDirContext(env);}
catch(NamingException e)
//System.out.println("Search failed. <br>");
return ctx;

} // end of method

} //end of class
Thanks for your help.
Joe Ess

Joined: Oct 29, 2001
Posts: 9168

[ November 25, 2003: Message edited by: Joe Ess ]

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Using the Singleton pattern
It's not a secret anymore!