Results 1 to 5 of 5
Thread: Exception Handling
- 06-20-2010, 06:11 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
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:
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 errorsJava 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()); }
i have also tried something like this:
NetBeans tells me the same error, and i get compile errors again.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()); }
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:
Thanks!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()); } } }
- 06-20-2010, 07:29 PM #2
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;
DirContext dirCtx = null;
- 06-20-2010, 07:35 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
norm,
i was hoping it was something easy like that!
Thank you so much!
- 06-21-2010, 11:18 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.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); } }
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.
- 06-21-2010, 03:09 PM #5
Member
- Join Date
- Jun 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Exception handling problem
By computerbum in forum New To JavaReplies: 2Last Post: 04-17-2010, 03:15 PM -
Exception Handling help
By MZA in forum New To JavaReplies: 3Last Post: 02-10-2010, 09:23 AM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 03:13 AM -
Jstl Exception Handling
By vamsidharpoosarla in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 07-18-2007, 06:17 AM -
JSTL Exception Handling
By chaatf in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-18-2007, 02:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks