NullPointerException对于FindById

时间:2019-10-10 18:46:40

标签: spring

似乎无法在我的项目中使用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);
    }

MainApp:

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>

0 个答案:

没有答案