Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:11:46+00:00 2026-05-26T14:11:46+00:00

Please take a look at the test class below. I am trying to do

  • 0

Please take a look at the test class below. I am trying to do an LDAP search with Spring LDAP Template. I am able to search and produce a list of entries corresponding to the search criteria without the Spring LDAP template by using the DirContext as shown in the method searchWithoutTemplate(). But when I use a LdapTemplate, I end up with a NPE as shown further below. I am sure I must be missing something. Can someone help please?

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
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;
import javax.naming.ldap.LdapName;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.DefaultDirObjectFactory;
import org.springframework.ldap.core.support.LdapContextSource;

public class LDAPSearchTest {
    //bind params
    static String url="ldap://<IP>:<PORT>";
    static String userName="cn=Directory Manager";
    static String password="password123";
    static String bindDN="dc=XXX,dc=com";

    //search params
    static String base = "ou=StandardUser,ou=XXXCustomers,ou=People,dc=XXX,dc=com";
    static String filter = "(objectClass=*)";
    static String[] attributeFilter = { "cn", "uid" };
    static SearchControls sc = new SearchControls();

    public static void main(String[] args) throws Exception {
        // sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
        sc.setReturningAttributes(attributeFilter);
        searchWithTemplate(); //NPE
        //searchWithoutTemplate(); //works fine
    }

    public static void searchWithTemplate() throws Exception {
        DefaultDirObjectFactory factory = new DefaultDirObjectFactory();
        LdapContextSource cs = new LdapContextSource();
        cs.setUrl(url);
        cs.setUserDn(userName);
        cs.setPassword(password);
        cs.setBase(bindDN);
        cs.setDirObjectFactory(factory.getClass ());
        LdapTemplate template = new LdapTemplate(cs);
        template.afterPropertiesSet();
        System.out.println((template.search(new LdapName(base), filter, sc,
                new AttributesMapper() {
                    public Object mapFromAttributes(Attributes attrs)
                            throws NamingException {
                        System.out.println(attrs);
                        return attrs.get("uid").get();
                    }
                })));
    }

    public static void searchWithoutTemplate() throws NamingException{
        Hashtable env = new Hashtable(11);
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, url);
        //env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, userName);
        env.put(Context.SECURITY_CREDENTIALS, password);
        DirContext dctx = new InitialDirContext(env);
        NamingEnumeration results = dctx.search(base, filter, sc);
        while (results.hasMore()) {
            SearchResult sr = (SearchResult) results.next();
            Attributes attrs = sr.getAttributes();
            System.out.println(attrs);
            Attribute attr = attrs.get("uid");
        }
        dctx.close();
    }
}

Exception is:

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:237)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:588)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:546)
    at LDAPSearchTest.searchWithTemplate(LDAPSearchTest.java:47)
at LDAPSearchTest.main(LDAPSearchTest.java:33)

I am using Spring 2.5.6 and Spring LDAP 1.3.0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:11:47+00:00Added an answer on May 26, 2026 at 2:11 pm

    A quick scan showed that it’s the authenticationSource field of AbstractContextSource that is the culprit. That file includes the following comment on the afterPropertiesSet() method:

    /**
    * Checks that all necessary data is set and that there is no compatibility
    * issues, after which the instance is initialized. Note that you need to
    * call this method explicitly after setting all desired properties if using
    * the class outside of a Spring Context.
    */
    public void afterPropertiesSet() throws Exception {
        ...
    }
    

    That method then goes on to create an appropriate authenticationSource if you haven’t provided one.
    As your test code above is most definitely not running within a Spring context, and you haven’t explicitly set an authenticationSource, I think you need to edit your code as follows:

    ...
    cs.setDirObjectFactory(factory.getClass ());
    
    // Allow Spring to configure the Context Source:
    cs.afterPropertiesSet();
    
    LdapTemplate template = new LdapTemplate(cs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at this code: template<class T> class A { class base
Can someone please take a look at the small test case below and tell
Please take a look at the html listed below and let me know why
Please take a look at the program below. Why am I getting an error?
Please take a look at this code: class Foo { public $barInstance; public function
THis is myDB Class //Please take a look at public Cursor getplaces() --- Am
Possible Duplicate: .prop() vs .attr() Please take a look at this fiddle: http://jsfiddle.net/DGzvP/ $(#test).prop({
Please take a look at this example: #include <iostream> #include <vector> #include <string> using
Please take a look at following code: #include <stdio.h> #include <iostream> using namespace std;
Please take a look at the following: jQuery.fn.jqPos = function(target, settings) { settings =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.