0

I am trying to query for an object from the database but I keep on getting a java.lang.NullPointerException error even when the data is present in the database. How do I fix this?

UserLogin POJO Class

public class UserLogin {

    private int usercode;
    private String userid;
    private String userpassword;
    private String userrole;
    private String username;
    private boolean useraccess;

    public UserLogin() {
    }

    public UserLogin(int usercode, String userid, String userpassword, String userrole, String username, boolean useraccess) {
        this.usercode = usercode;
        this.userid = userid;
        this.userpassword = userpassword;
        this.userrole = userrole;
        this.username = username;
        this.useraccess = useraccess;
    }
//getters and setters...

DAO Class:

@Transactional
@Repository("daoUser")
public class DaoUser{

    private JdbcTemplate jdbcTemplate;

    public UserLogin getUserlogin(final String userid) {
        UserLogin user = new UserLogin();
        try {
            String sql = "SELECT usercode, userid, userpassword, userrole, username, useraccess FROM myschema.userlogin WHERE userid=?";
            Object[] criteria = { userid };
            user = jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(UserLogin.class), criteria);
            
        } catch (Exception e) {
            System.out.println("Error in DaoUser.getUserlogin(final String userid) : " + e);
        }
        return user;
    }
}

The Error:

Error in DaoUserManagement.getUserlogin(final String userid) : java.lang.NullPointerException

The Data I'm trying to fetch:

usercode    userid  userpassword    userrole    username    useraccess

3           userA   userpassword    A           username    true
inquisitive
  • 135
  • 2
  • 11
  • What is the exact place where NullPointerException occurs? Please add stacktrace and the sample row data that you are trying to fetch. The probable error that some of fetched values (that come from DB) are NULL, and thus cat't be converted to primitive arguments of UserLogin constructor - `int usercode` or `boolean useraccess`. Primitive values can't hold `null`, so if `null` is possible it's better to use wrappers (Integer and Boolean) – Nikolai Shevchenko Nov 02 '21 at 06:51
  • @NikolaiShevchenko I have updated my question, I don't have any null values in the mentioned columns – inquisitive Nov 02 '21 at 07:02
  • This may sound like a silly question, but have you debugged the code to ensure `userid` is not null? – mohammedkhan Nov 02 '21 at 10:36
  • Add `@Autowired` to your `JdbcTemplate` field or make the field `final` and add a constructor to assign the `JdbcTemplate` through constructor injection. – M. Deinum Nov 02 '21 at 14:27

1 Answers1

1

Where do you initialise jdbcTemplate object? So the actual null pointer exception is at this object.

Use @Autowired or do the constructor injection.

@Autowired
private JdbcTemplate jdbcTemplate
Alien
  • 15,141
  • 6
  • 37
  • 57