Announcement Announcement Module
No announcement yet.
null pointer exception for loadUserByUserName Page Title Module
Move Remove Collapse
This topic is closed
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • null pointer exception for loadUserByUserName

    * Problem solved, delete thread *
    Last edited by cablepuff; Mar 13th, 2008, 09:58 AM.

  • #2
    here is my java code UserServiceImpl class

    public class UserDetailsServiceImpl implements UserDetailsService {
        private UserDao userDao;
        private Logger log = Logger.getLogger(getClass());
        public UserDetails loadUserByUsername(String username)
                throws UsernameNotFoundException, DataAccessException {
  "trying to find " + username);
            User u = userDao.getUserByName(username);
            if (u == null)
                throw new UsernameNotFoundException(username + " is not found");
            boolean active = !u.getStatus().equals(Status.CANCEL);
            boolean enable = u.getStatus().equals(Status.ACTIVE);
            List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            for (UserGroup ug : u.getUserGroups())
            return new
            org.acegisecurity.userdetails.User(u.getName(), u.getPassword(),
                                               enable, active, active, active,
                                               authorities.toArray(new GrantedAuthority[0]));
    Last edited by cablepuff; Aug 18th, 2008, 10:12 PM.


    • #3
          <bean id="userDetailsServiceImpl" class=""/>
      You are not injecting a UserDao into UserDetailServiceImpl.


      • #4
        Originally posted by mathessd View Post
            <bean id="userDetailsServiceImpl" class=""/>
        You are not injecting a UserDao into UserDetailServiceImpl.
        The UserDao has the @Resource annotation, so it could also be that you're not using <context:annotation-config />.


        • #5
          So what was the solution to this problem?

          I got a loadUserByUsername which calls a method from a DAO to get a User (which implements UserDetails) from the database. When calling loadUserByUsername all data is correct (such as user.getAuthorities and so on) but when logging in with the default spring security login I receive a NullPointer exception.

          To bad someone finds out how to solve his problem and does not write the solution in the according thread.


          • #6
            The problem is described above - failure to inject a resource. Since you are apparently able to load user data, the only thing your problem has in common is a NPE. Without the stacktrace (and preferably the debug log) it's unlikely anyone will be able to help you fix things. Please attach those and someone might be able to suggest a solution.


            • #7
              Thank you for your reply.
              I did not want to 'highjack' this thread thus I haven't attached any additional files. In fact I wasn't even sure if I had the same problem but was a bit frustrated that I couldn't see the solution straight ahead, sorry!

              For my problem: I was using the spring-security-core library 2.0.1 so no EntityManager was created (see with the current snapshot of the library the problem does not exist anymore.

              Best wishes
              Last edited by mgood; May 18th, 2008, 09:02 AM. Reason: Added link to Bug SEC-750