gpt4 book ai didi

java - 面临空指针异常

转载 作者:行者123 更新时间:2023-11-30 03:29:06 27 4
gpt4 key购买 nike

编译时遇到空指针异常。

public class LDAP_MultiUser_Copy1 {
public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory";
public static String MY_HOST = "ldap://singh.plm.com:3890";

public static String MY_SEARCHBASE = "cn=Public,x=X-Y,ou=people,cn=Windchill_8.0,cn=Application Services,x=x-y";
public static String MY_FILTER = "(&(objectclass=groupOfUniqueNames)(uniqueMember=";
public static String MGR_DN = "cn=Manager";
public static String MGR_PW = "ldapadmin";
//public String[] MY_FILTER2=new String[200];
public static void main(String[] args) throws IOException {
String[] MY_FILTER2=new String[900];
String[] First_Name=new String[200];
//String[] MY_FILTER2=null;
try{
String fileName="C:\\report\\report.txt";
FileReader inputFile = new FileReader(fileName);
BufferedReader bufferReader = new BufferedReader(inputFile);
String line;

FileWriter writer = new FileWriter("C:\\report\\report11.csv");
PrintWriter pwr = new PrintWriter(writer);

String fileName1="C:\\report\\report1.txt";
FileReader inputFile1 = new FileReader(fileName1);
BufferedReader bufferReader1 = new BufferedReader(inputFile1);
String line1;



int i=0;
while (((line = bufferReader.readLine()) != null) && (line1 = bufferReader1.readLine())!= null)
{
//System.out.println(line);
//System.out.println("Reading text file");
MY_FILTER2[i]=line;
//System.out.println("My_Filter2="+MY_FILTER2[i]);


//First_Name = line.split(",");

//System.out.println("MAking my_filter_final");
String MY_FILTER_Final=MY_FILTER+line+"))";
//System.out.println("MY_FILTER_Final="+MY_FILTER_Final);


Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,INITCTX);
env.put(Context.PROVIDER_URL,MY_HOST);
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,MGR_DN);
env.put(Context.SECURITY_CREDENTIALS,MGR_PW);
DirContext ctx = new InitialDirContext(env);
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
//performs the actual search
//We give it a searchbase, a filter and the contraints containing the scope
//of the search
NamingEnumeration results = ctx.search(MY_SEARCHBASE,MY_FILTER_Final,constraints);
//now stop through the search results
//System.out.println(results);
while(results==null)
{
System.out.println("**************************************"+line+"$$$$$does not exit in USerlist");
}
while(results != null || results.hasMore()){
System.out.println("entering wile loop----------------");
SearchResult sr = (SearchResult)results.next();
String dn = sr.getName();
String state = dn.substring(dn.indexOf("cn=")+3);
//String First_Name_Final=First_Name[i].substring(First_Name[i].indexOf("cn=")+3);

//System.out.println("Username="+line1+"@@@@@@@"+line+" Group = "+state);
System.out.println("Username="+line1+"-->Group = "+state);
pwr.print(line1);
pwr.print(",");
pwr.print(state);
pwr.print(",");
pwr.println("");
i++;
Attributes attrs = sr.getAttributes();
NamingEnumeration ne = attrs.getAll();



if (attrs == null) {
System.out.println("Entry " + dn + " has none of the specified attributes\n");
return;
}

}
}
pwr.close();
}catch(Exception ex){
ex.printStackTrace();
System.exit(1);

}
}
}

在 while (((line = bufferReader.readLine()) != 中遇到空指针异常线。当用户不属于任何组时,它会遇到空指针异常。想要的是当用户不属于任何组时,它应该返回不存在。

输出:-

entering wile loop----------------
java.lang.NullPointerException
at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.getNextBatch(Unknown Source)
at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.nextAux(Unknown Source)
at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.nextImpl(Unknown Source)
at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.next(Unknown Source)
at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.next(Unknown Source)
at com.LDAP_MultiUser_Copy1.main(LDAP_MultiUser_Copy1.java:89)

最佳答案

从代码角度来看,while(results != null || results.hasMore()){应该是while(results != null && results.hasMore()){ 。否则的话,如果 results 很可能会导致 NPE。没有元素。

关于java - 面临空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29468824/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com