2015-04-06 54 views
1

編譯時遇到空指針異常。面向空指針異常

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

    } 
    } 
    } 

在同時面臨空指針異常(((行= bufferReader.readLine())!= 線。當用戶不屬於任何組它擊中一個空指針異常。 什麼是想要的是當用戶不屬於任何組,它應該返回不存在

OUTPUT: - 。

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) 
+1

這是您的**行號碼89 **? – R9J 2015-04-06 09:50:37

+0

是的你是對的。行號89. SearchResult sr =(SearchResult)results.next(); – 2015-04-06 10:45:25

回答

0
import javax.naming.directory.*; 
    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; 

     pwr.print("User"); 
     pwr.print(","); 
     pwr.print("Group Name"); 
     pwr.print(","); 
     pwr.println(); 

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



    if((results.hasMore())==false) 
    { 
     System.out.println("%%%%%%%%%No Group Presnt for the USer="+line1); 
     pwr.print(line1); 
     pwr.print(","); 
     pwr.print("No Org Group"); 
     pwr.print(","); 
     pwr.println(); 
    } 

    while(results != null && results.hasMore()){ 
     System.out.println("entering wile loop----------------"); 


     SearchResult sr = (SearchResult)results.next(); 
     if(sr==null) 
     { 
      System.out.println(); 
     } 

    /* if((SearchResult sr = (SearchResult)results.next())==null) 
     { 
      System.out.println("No Org Group"); 
      pwr.print(line1); 
      pwr.print(","); 
      pwr.print("No Org Group"); 
      pwr.print(","); 
      pwr.println(""); 
     }*/ 
     else 
    { 
    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(); 
    /*while(ne.hasMoreElements()) { 
    Attribute attr = (Attribute) ne.next(); 
    String attrID = attr.getID(); 
    System.out.println(attrID+" :"); 
    Enumeration vals = attr.getAll(); 
    while(vals.hasMoreElements()) { 
    System.out.println("\t"+vals.nextElement()); 
    } 
    }*/ 
    //System.out.println("\n"); 

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

    } 
    } 
    } 
+0

這是你問題的答案嗎?如果是這樣,請解釋你做什麼來解決你的問題,否則,如果你想在問題中修改你的問題使用編輯鏈接並更新你的問題。 – 2015-04-07 10:30:12

+0

我正面臨空行指針異常: - SearchResult sr =(SearchResult)results.next();所以我需要確保SearchResult沒有傳遞null值,所以如果((results.hasMore())== false) {....}這個循環處理可以將空值傳遞給SearchResult的情況。因此SearchResult不會面向空值。因此不會出現例外情況。它執行我需要它的任務 – 2015-04-09 07:45:32

+0

在答案中添加解釋而不是在評論中添加。 – 2015-04-09 07:58:44

2

從代碼的角度來看,while(results != null || results.hasMore()){應該是while(results != null && results.hasMore()){否則,很可能導致NPE如果012。沒有元素。

+0

其實你是對的。但在這種情況下,我試圖逮捕SearchResult sr =(SearchResult)results.next()的情況;找不到結果。在這種情況下,我需要知道沒有找到結果。它只是跳過並移動到新的查詢。如何查找未返回組的用戶? – 2015-04-06 12:52:16