spring - 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>
解决方案
推荐阅读
- angular - 隐藏密码字段内的显示眼睛图标角度 6
- java - 检查链接以了解它是否是 android/java 中的图像
- javascript - onkeypress 不适用于 asp.net webform 中的文本框
- jquery - 它不是我在 ajax 中给出的控制器功能
- java - 将数组列表中的项目拆分为字符串数组时出现问题
- vba - 如何将 VBA 中的日期格式更改为 dd mm yy?
- c - 如何在结构中包含宏
- vba - 如何在循环中引用同一列中的单元格,vba
- swift - 如何设置子 ASNetworkImageNode 的比例
- javascript - 在ngrx中的订阅内管道