Announcement Announcement Module
Collapse
No announcement yet.
getJdbcTemplate always returning null Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • getJdbcTemplate always returning null

    I have the following impl method in my mvc app. When I return to the controller the NPE exception always is executed. The user I am logging in with is in the users table. My users table has the following columns: USER_ID, FIRSTNAME, LASTNAME, USERNAME, PASSWORD, EMAIL

    Impl Class:
    Code:
    public User getUser(User user) {
    		String sql = "SELECT * FROM users WHERE USERNAME = ? and PASSWORD = ?";
    		return (User) getJdbcTemplate().queryForObject(sql, new UserRowMapper(), new Object[] { user.getUsername(), user.getPassword() });
    	}
    Controller:
    Code:
    @RequestMapping(value="login", method = RequestMethod.POST)
    	public ModelAndView processLogin(@ModelAttribute("login") User user, BindingResult result) {
    		User loginUser = user;
    		ModelAndView mav = new ModelAndView("login"); 
    		ModelAndView mavsuccess = new ModelAndView("loginsuccess");
    		user.setValid(false);
    		loginValidation.validate(loginUser, result);
    		if (result.hasErrors()) {
    			mav.addObject("login", loginUser);
    			return mav;
    		}
    		else {
    			try {
    		   		loginUser = loginService.getUser(loginUser);
    			}
    			catch(NullPointerException e) {	System.out.println("Username "+loginUser.getUsername()+" not found: "+e); }
    			catch(EmptyResultDataAccessException e) { return null; }
    		    catch(Exception e) { System.out.println("Exception: "+e); }
    			if(loginUser == null) {
    				result.rejectValue("username", "invalidUser.user.username", "Username not found.  Please register.");
    				return mav;
    			}
    			else
    				return mavsuccess;
    		}
    	}

    Stack Trace:

    Code:
    Username test not found: java.lang.NullPointerException

    UserRowMapper Class:

    Code:
    public class UserRowMapper implements RowMapper<User>{
        private static final String FIRSTNAME = "FIRSTNAME";
        private static final String LASTNAME = "LASTNAME";
        private static final String USERNAME = "USERNAME";
        private static final String PASSWORD = "PASSWORD";
        private static final String EMAIL = "EMAIL";
    
    	public User mapRow(ResultSet rs, int rowNum) throws SQLException {
    		User user = new User();
    		user.setFirstname(rs.getString(FIRSTNAME));
    		user.setLastname(rs.getString(LASTNAME));
    		user.setEmail(rs.getString(EMAIL));
    		user.setUsername(rs.getString(USERNAME));
    		user.setPassword(rs.getString(PASSWORD));
    		return user;
    	}
    }
    Any help with this would be appreciated. I have a test app that is imitating the login and it finds the user.

    Is it because of the way i'm handling the exceptions in my controller?

    Code:
    public class LoginApp 
    {
        public static void main(String[] args)
        {
        	ApplicationContext context = new ClassPathXmlApplicationContext("servlet-context.xml");
        	LoginServiceImpl loginService = (LoginServiceImpl) context.getBean("loginServiceImpl");
        	User user1 = new User("test", "test", "[email protected]", "test", "test", "test");
        	
        	User user1Valid = loginService.getUser(user1);
        
        	System.out.println("User1"+user1Valid.toString());
        }
    }
    Output
    User1Firstname: test
    Lastname: test
    Email: [email protected]
    Username: test
    Password: test

  • #2
    Anyone know what it could be?

    Comment


    • #3
      Post the full stacktrace and configuration. My guess you are creating a new instance of your service in your controller instead of using the one from the application context.

      Comment

      Working...
      X