0
 This is my spring boot v3.10 api, a post request to register user and check if email already exist.

But i got this error: "java.lang.NullPointerException: Cannot invoke "org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(String, java.util.Map, java.lang.Class)" because "this.jdbc" is null"

this is the class that creates a new user but before creating it checks if an email already exist in the user table with the getEmailCount() method but this where i get the error.

@Repository
@RequiredArgsConstructor
@Slf4j
public class UserRepositoryImplementation implements UserRepository<User> {
    public NamedParameterJdbcTemplate jdbc;
    private final RoleRepository<Role> roleRepository;
    private final BCryptPasswordEncoder encoder;

    @Override
    public User create(User user) {
        // check if email is unique
        if(getEmailCount(user.getEmail().trim().toLowerCase()) > 0) {
            throw new ApiException("Email already in use. Please use a different email and try again.");
        }
        // save new user
        try{
            KeyHolder holder = new GeneratedKeyHolder();
            SqlParameterSource parameters = getSqlParameterSource(user);
            jdbc.update(INSERT_USER_QUERY, parameters, holder);
            user.setId(requireNonNull(holder.getKey()).longValue());

            // add role to the user
            roleRepository.addRoleToUser(user.getId(), ROLE_USER.name());

            // send verification URL
            String verificationUrl = getVerificationUrl(UUID.randomUUID().toString(), ACCOUNT.getType());

            // save URL in verification table
            jdbc.update(INSERT_VERIFICATION_QUERY, Map.of("userId", user.getId(), "url", verificationUrl));

            // send email to user with verification URL
            //emailSerivice.sendVerificationUrl(user.getFirstName(), user.getEmail(), verificationUrl, ACCOUNT);
            user.setEnabled(false);
            user.setNotLocked(true);

            // return the newly created user
            return user;
            // if any errors, throw exception with proper message
        }catch (Exception exception){}
        throw new ApiException("An error occurred. Please try again.");
    }

This is the method that handles the query to check if the email already exist:

 private Integer getEmailCount(String email){
        return jdbc.queryForObject(COUNT_USER_EMAIL_QUERY, Map.of("email", email), Integer.class);
    }

As error said the jdbc is null. This the query for the COUNT_USER_EMAIL_QUERY variable: COUNT_USER_EMAIL_QUERY = "SELECT COUNT(*) FROM Users WHERE email = :email";

1 Answers1

0

"jdbc" is not injected as a bean and not initialized, therefore you are getting NullPointerException. Replace

public NamedParameterJdbcTemplate jdbc;

with

private final NamedParameterJdbcTemplate jdbc;
SwathiP
  • 315
  • 3
  • 5
  • I think you are right the access modifier was a actually the problem. But after changing it which i think makes sense actually output the last exception: catch (Exception exception){} throw new ApiException("An error occurred. Please try again."); which means it does not run the code. – Sheriff Sulemana Jul 24 '23 at 20:33
  • The last exception you received seems to be related to something within your repository implementation when creating a user. You may have to debug each line and find out where exactly it fails. If you provide complete stack trace, I can probably help. Could you mark my answer as accepted since it solves the original issue? – SwathiP Jul 24 '23 at 20:44
  • 1
    After series of debugging i have been able to get to somewhere interesting and very inciting though i haven't got the final solution yet but i have got the idea where it's leading me to, am taking a break. Thanks so much i highly appreciate your time for me i wish i could double accept your answer. – Sheriff Sulemana Jul 25 '23 at 01:05