你好,我使用springboot的时候,使用的是jdbcTemplate(mybatis太麻烦,不用了),在执行查询语句时,出现了空指针错误,我的IDEA报的空指针错误是这样的
java.lang.NullPointerException: null
这是为什么呢?
附代码
bean代码
package com.example.demo.bean;
public class Cangpin {
private long xuhao;
private String path2;
public String getPath2() {
return path2;
}
public void setPath2(String path2) {
this.path2 = path2;
}
private String sys;
public long getXuhao() {
return xuhao;
}
public void setXuhao(long xuhao) {
this.xuhao = xuhao;
}
public String getSys() {
return sys;
}
public void setSys(String sys) {
this.sys = sys;
}
public String getCangpinname() {
return cangpinname;
}
public void setCangpinname(String cangpinname) {
this.cangpinname = cangpinname;
}
public double getYuanjia() {
return yuanjia;
}
public void setYuanjia(double yuanjia) {
this.yuanjia = yuanjia;
}
public double getXianjia() {
return xianjia;
}
public void setXianjia(double xianjia) {
this.xianjia = xianjia;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
private String cangpinname;
private double yuanjia;
private double xianjia;
private int num;
}
这是controller代码
package com.example.demo.controller;
import com.example.demo.bean.Cangpin;
import com.example.demo.dao.CangpinDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class CangpinController {
@Autowired
private CangpinDao cpDao;
@RequestMapping("/save")
public String getCpDao() {
Cangpin cp=new Cangpin();
cp.setXuhao(1);
cp.setPath2("da");
cp.setSys("ss");
cp.setCangpinname("银币");
cp.setYuanjia(188.0);
cp.setXianjia(18.0);
cp.setNum(8844);
cpDao.save(cp);
return "插入成功";
}
@RequestMapping("/check")
public void getCheck()
{
JdbcTemplate jdbcTemplate=null;
String sql="select * from cangpin where xuhao =?";
Cangpin cp = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Cangpin>(Cangpin.class),1);
System.out.println(cp.getCangpinname());
}
}
这是DAO层代码
package com.example.demo.dao;
import com.example.demo.bean.Cangpin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.ArrayList;
@Repository
public class CangpinDao {
@Resource(name="jdbcTemplate")
@Autowired
private JdbcTemplate jdbcTemplate;
public void save(Cangpin cangpin){
jdbcTemplate.update("insert into cangpin(xuhao,path2,sys,cangpinname,yuanjia,xianjia,num) values (?,?,?,?,?,?,?)",
cangpin.getXuhao(),cangpin.getPath2(),cangpin.getSys(),cangpin.getCangpinname(),
cangpin.getYuanjia(),cangpin.getXianjia(),cangpin.getNum());
}
}
我启动springboot,是能够正常启动的,但是输入相应的地址路径,就会出现空指针异常,这是为什么呢?