Getting a NullPointer Exception when I Run the program. Please help !!
Code here:
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class LdapTest {
public static void main(String[] args) throws Exception
{
final String PROVIDER_URL = "http://localhost:1389/dc=vysbank,dc=com"; //Enter LDAP URL here
final Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, PROVIDER_URL);
try {
DirContext ctx = new InitialDirContext(env);
String filter = "(uid=1234)"; // Enter User ID here.
String[] attrIDs = {"uid","cn","mail"}; // Enter list of attributes to retrieve from LDAP here
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
ctls.setReturningAttributes(attrIDs);
NamingEnumeration answer = ctx.search("ou=People", filter, ctls);
SearchResult searchResult = null;
String cn=null;
String uid=null;
String mail=null;
while (answer.hasMore()) {
searchResult = (SearchResult) answer.next();
Attributes attr = searchResult.getAttributes();
cn=attr.get("cn").get(0).toString();
uid=attr.get("uid").get(0).toString();
mail=attr.get("mail").get(0).toString();
System.out.println("Name: "+cn);
System.out.println("User ID: "+uid);
System.out.println("E-mail Address: "+mail);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Null pointer Execption is thrown when an application attempts to use null in a case where an object is required.
Below snippet throws NPE because you are calling a method on String a which is not properly initialized.
String a = null;
a.toString();
Debug your code to find such scenario.
You probably have null
pointers here:
searchResult = (SearchResult) answer.next();
Attributes attr = searchResult.getAttributes();
cn=attr.get("cn").get(0).toString();
uid=attr.get("uid").get(0).toString();
mail=attr.get("mail").get(0).toString();
I would do null
pointer checks to see if any values are null, and if so, give default empty string values:
if(attr != null){
if(attr.get("cn") != null && attr.get("cn").get(0) != null)
cn=attr.get("cn").get(0).toString();
else
cn = "";
if(attr.get("uid") != null && attr.get("cn").get(0) != null)
uid=attr.get("uid").get(0).toString();
else
uid = "";
if(attr.get("mail") != null && attr.get("cn").get(0) != null)
mail=attr.get("mail").get(0).toString();
else
mail = "";
}
However, this all depends on the goal of the code. If you are receiving values from a front end (say a web page), you should always to empty or null
checks. If you are creating variables and plan to use them, make sure those variables contain values by the time you use them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.