Announcement Announcement Module
Collapse
No announcement yet.
Problems with creating my first search Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with creating my first search

    Hi!

    I installed the Apache Directory Server on my pc and I a downloaded the spring ldap 1.3. I got the following code from the spring introduction http://static.springsource.org/sprin...roduction.html

    Code:
    import java.util.List;
    
    import javax.naming.NamingException;
    import javax.naming.directory.Attributes;
    
    import org.springframework.ldap.core.AttributesMapper;
    import org.springframework.ldap.core.LdapTemplate;
    import org.springframework.ldap.core.support.LdapContextSource;
    
    public class start {
    	public static void main(String[] args) throws Exception {
    		LdapContextSource ldap = new LdapContextSource();
    		ldap.setUserDn("uid=admin,ou=system");
    		ldap.setPassword("passw");
    		ldap.setUrl("ldap://localhost:389");
    		ldap.setBase("dc=example,dc=com");
    
    		LdapTemplate ldapTemplate = new LdapTemplate(ldap);
    
    		List list = ldapTemplate.search("", "(objectclass=*)", new AttributesMapper() {
    			public Object mapFromAttributes(Attributes attrs) throws NamingException {
    				return attrs.get("cn").get();
    			}
    		});
    	}
    }
    When I run the application I get the following nullpointer exception.

    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(AbstractContextSource.java:125)
    	at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:287)
    	at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:259)
    	at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:571)
    	at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:556)
    	at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:411)
    	at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:431)
    	at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:451)
    	at start.main(start.java:20)
    Can someone tell me what I am doing wrong here?

    Thanks
    Styssi

  • #2
    Re: Problems with creating my first search

    Styssi,

    You forgot to call the afterPropertiesSet method on your context after setting all the required properties. Try this:

    Code:
                    LdapContextSource ldap = new LdapContextSource();
    		ldap.setUserDn("uid=admin,ou=system");
    		ldap.setPassword("passw");
    		ldap.setUrl("ldap://localhost:389");
    		ldap.setBase("dc=example,dc=com");
    
    		ldap.afterPropertiesSet();
    Spring framework would invoke this method automatically if you had the context created and managed as a Spring bean.

    Comment


    • #3
      problem solved

      thanks for your help. Your solution solved the problem

      Comment

      Working...
      X