Results 1 to 5 of 5
  1. #1
    liljester is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default Exception Handling

    Greetings,

    Im working on a small project to connect to an ldap server and fetch the attributes of a given dn. Now I have been able to make it work with no user input as its not that difficult and the entire bit of code was in a try/catch. But now i want to add in a basic menu and capture some user input.

    Logically (to me) the program can be broken into a few pieces. The first bit i want to handle exceptions for is the initial construction of the DirContext object.. like so:

    Java Code:
    try {
    	DirContext dirCtx = new InitialDirContext(environment);
    	System.out.printf("Connected to: %s\n", connectionString);
    } catch (NamingException e) {
    	System.out.printf("Connection Error: %s\n", e.toString());
    }
    now if i do it this way, when i try to use dirCtx later in the program NetBeans tells me "variable dirCtx might not have been initialized", and there are compile errors

    i have also tried something like this:
    Java Code:
    DirContext dirCtx;
    
    try {
    	dirCtx = new InitialDirContext(environment);
    	System.out.printf("Connected to: %s\n", connectionString);
    } catch (NamingException e) {
    	System.out.printf("Connection Error: %s\n", e.toString());
    }
    NetBeans tells me the same error, and i get compile errors again.

    I'm very new to java, but have extensive experience with php, and even a little c# experience. I know this is probably a very simple problem that im just not seeing the answer to, but i am unable to locate the answer in the java book i have, also ive read the java tutorial on sun's site and still not sure why the way im doing it is wrong.

    i guess im looking for a quick explanation why the way im doing it is wrong, and what the proper way to do it is.

    full code:
    Java Code:
    import java.util.*;
    import javax.naming.*;
    import javax.naming.directory.*;
    
    /**
     *
     * @author Jerry
     */
    public class Main {
    
        public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		String connectionString = "ldap://192.168.137.8:389/CN=Portal,O=Jenzabar,C=US";
    		String userFQDN = "CN=173,OU=PortalUsers,CN=Portal,O=Jenzabar,C=US";
    		String userPassword = "password";
    
    		Hashtable environment = new Hashtable();
    		//DirContext dirCtx;
    		try {
    			environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    			environment.put(Context.PROVIDER_URL, connectionString);
    			environment.put(Context.SECURITY_PRINCIPAL,userFQDN);
    			environment.put(Context.SECURITY_CREDENTIALS,userPassword);
    			DirContext dirCtx = new InitialDirContext(environment);
    			System.out.printf("Connected to: %s\n", connectionString);
    		} catch(NamingException e) {
    			System.out.printf("Connection Error: %s\n", e.toString());
    		}
    
    		System.out.println("What would you like to do? (enter a number)");
    		System.out.println("1 - Print Attributes of DN");
    		String command = input.next();
    
    		if(command.equals("1")) {
    			System.out.print("Enter DN:");
    			String dn = input.next();
    			printAttributes(dirCtx, dn);
    		}
    
    
    
        }
    
    	public static void printAttributes(DirContext dirCtx, String dn) {
    		try {
    			Attributes userAttributes = dirCtx.getAttributes(dn);
    			int i = 0;
    			for(NamingEnumeration allAttribs = userAttributes.getAll(); allAttribs.hasMore();) {
    				Attribute thisAttrib = (Attribute)allAttribs.next();
    				System.out.printf("%s: ", thisAttrib.getID());
    
    				for(NamingEnumeration value = thisAttrib.getAll(); value.hasMore();) {
    					if(i == 0) {
    						System.out.printf("%s", value.next());
    					} else {
    						System.out.printf(", %s", value.next());
    					}
    
    					i++;
    				}
    				System.out.println();
    				i = 0;
    			}
    		} catch (NamingException e) {
    			System.out.printf("Error Fetching Attributes for '%s': %s\n", dn, e.toString());
    		}
    	}
    
    }
    Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,584
    Rep Power
    23

    Default

    DirContext dirCtx;
    This defines the variable but does NOT assign it any value. Because you need to define the variable outside the scope of the try{} block, you can do this to give it a value:
    DirContext dirCtx = null;

  3. #3
    liljester is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    norm,

    i was hoping it was something easy like that!

    Thank you so much!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Java Code:
    		DirContext dirCtx = null;  // I filled this in based on Norms post.
    		try {
    		    // Get the dirCtxt.
    		} catch(NamingException e) {
    			System.out.printf("Connection Error: %s\n", e.toString());
    		}
    
    		System.out.println("What would you like to do? (enter a number)");
    		System.out.println("1 - Print Attributes of DN");
    		String command = input.next();
    
    		if(command.equals("1")) {
    			System.out.print("Enter DN:");
    			String dn = input.next();
    			printAttributes(dirCtx, dn);
    		}
        }
    You've got yourself a bug here. If the lookup for the context fails you still merrily continue on, albeit with a small message on the screen. This will result in a null pointer exception (since dirCtx is null) in printAttributes.

    Since you cannot do anything in this program without a valid dirCtx this is one of those situations you want to crash. That is throw the NamingException.

  5. #5
    liljester is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default

    Tolls,

    Thank you for pointing that out. I should be able to change the flow of the program to take into account a null context.

    Thanks!

Similar Threads

  1. Exception handling problem
    By computerbum in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 03:15 PM
  2. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 AM
  3. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM
  4. Jstl Exception Handling
    By vamsidharpoosarla in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-18-2007, 06:17 AM
  5. JSTL Exception Handling
    By chaatf in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-18-2007, 02:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •