writing a common service to get user details by username as below:
package com.pustaknotika.common;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import com.pustaknotika.users.mapper.UsersRowMapper;
import com.pustaknotika.users.pojo.Users;
@Repository("CommonServices")
public class CommonservicesImpl implements ICommonservices {
private static Logger LOGGER = LoggerFactory.getLogger(CommonservicesImpl.class);
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;
@Autowired
private Environment env;
@Override
public Users getUserByUserName(String userName)
{
LOGGER.info("{}", env);
String sql= env.getProperty("GET_USERDETAILS");
Map<String, Object> args = new HashMap<String, Object>();
args.put("UserName", UserName);
Users user = null;
List<Users> lstUsers = null;
try {
lstUsers = jdbcTemplate.query(sql, args, new UsersRowMapper());
// logger
LOGGER.info("{} USer(s) Selected", lstUsers.size());
}catch (Exception e) {
e.printStackTrace();
}
if (lstUsers.size()>0) {
user = lstUsers.get(0);
}
return user;
}
}
When i call this service, I am facing error
java.lang.NullPointerException: Cannot invoke “org.springframework.core.env.Environment.getProperty(String)” because “com.pustaknotika.common.CommonservicesImpl.env” is null
What am i missing here
i could try multiple options but nothing worked out.
New contributor
Sarfaraj Pirjade is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.