用于ldap连接的java程序中的NullPointerException

时间:2014-01-13 05:01:59

标签: java nullpointerexception

运行程序时获取NullPointer异常。请帮忙 !!

代码在这里:

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();
        }

    }

}

2 个答案:

答案 0 :(得分:1)

当应用程序在需要对象的情况下尝试使用null时,抛出

Null pointer Execption

在snippet下面抛出NPE,因为你在String a上调用了一个未正确初始化的方法。

     String a = null;
     a.toString();

调试代码以找到这种情况。

答案 1 :(得分:1)

你可能在这里有null个指针:

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();

我会做null指针检查以查看是否有任何值为null,如果是,则给出默认的空字符串值:

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 = "";
}

但是,这一切都取决于代码的目标。如果您从前端(例如网页)接收值,则应始终清空或null检查。如果要创建变量并计划使用它们,请确保这些变量在您使用它们时包含值。