Announcement Announcement Module
Collapse
No announcement yet.
null pointer exception for loadUserByUserName Page Title Module
Move Remove Collapse
This topic is closed
X
X
Conversation Detail Module
Collapse
  • 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

    Code:
    package company.core.security.service.impl;
    
    public class UserDetailsServiceImpl implements UserDetailsService {
        @Resource
        private UserDao userDao;
        private Logger log = Logger.getLogger(getClass());
        @Override
        public UserDetails loadUserByUsername(String username)
                throws UsernameNotFoundException, DataAccessException {
            log.info("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())
            {
                authorities.add(ug.getGroup());
            }
            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.

    Comment


    • #3
      Code:
          <bean id="userDetailsServiceImpl" class="citysearch.core.security.service.impl.UserDetailsServiceImpl"/>
      You are not injecting a UserDao into UserDetailServiceImpl.

      Comment


      • #4
        Originally posted by mathessd View Post
        Code:
            <bean id="userDetailsServiceImpl" class="citysearch.core.security.service.impl.UserDetailsServiceImpl"/>
        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 />.

        Comment


        • #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.

          Comment


          • #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.

            Comment


            • #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 http://jira.springframework.org/browse/SEC-750).. with the current snapshot of the library the problem does not exist anymore.

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

              Comment

              Working...
              X