首页 > 解决方案 > FindById 的 NullPointerException

问题描述

似乎无法让 FindById 在我的项目中工作。无论我尝试什么,它都会不断给出相同的错误,它可能只是一些小问题,但无法弄清楚。

编辑:添加 configbean.xml

RowMapper
public class CustDetailsRowMapper implements RowMapper<CustDetails> {

     CustDetails CustDetails;
    @Override
    public CustDetails mapRow(ResultSet rs, int rowNum) throws SQLException {

         CustDetails.setCustName(rs.getString("CUSTNAME"));
         CustDetails.setAddress(rs.getString("CUSTADDRESS"));
         CustDetails.setCustId(rs.getInt("CUSTID"));
        return CustDetails;
    }

DAO 代码:

@Repository 
public class CustDetailsDaoImplementation implements CustDetailsDAO {

    private JdbcTemplate jdbcTemplate; 

    @Override
    public CustDetails findById(int custId) {
        String sql = "Select * FROM CUSTOMERDETAILS WHERE CUSTID = ?";
        CustDetails custDetails = jdbcTemplate.queryForObject(sql, new CustDetailsRowMapper(), custId);
        return custDetails;
    }

服务代码:

   public class CustDetailsServiceImplementation implements custDetailsService {
      @Autowired
      CustDetailsDAO custDetailsDAO;

    @Override
    public List<CustDetails> getAllCusts() {
        // TODO Auto-generated method stub
        return (List<CustDetails>) custDetailsDAO.findAll();
    }

    @Override
    public CustDetails getACustByItsId(int custId) {
        // TODO Auto-generated method stub
        return custDetailsDAO.findById(custId);
    }

主应用:

public class MainApp {

  public static void main(String[] args) {
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("configBean.xml");

       custDetailsService CustDetailsService  = (custDetailsService) context.getBean("custDetailsServiceImplementation");
       System.out.println(CustDetailsService.getACustByItsId(1));

  }

似乎无法修复以下错误:

Exception in thread "main" java.lang.NullPointerException
    at ie.sean.dao.CustDetailsDaoImplementation.findById(CustDetailsDaoImplementation.java:26)
    at ie.sean.services.CustDetailsServiceImplementation.getACustByItsId(CustDetailsServiceImplementation.java:26)
    at ie.sean.domain.MainApp.main(MainApp.java:14)

configBean.xml 代码:

    <context:component-scan base-package="ie.sean" />

<jdbc:embedded-database id="dataSource" type="H2">
    <jdbc:script location="schema.sql" />
    <jdbc:script location="data.sql" />
</jdbc:embedded-database>

<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"  autowire="byType" />

</beans>

标签: spring

解决方案


推荐阅读