Java namedParameterjdbcTemplate提供空指针异常
我一直在做一个简单的程序,使用jdbc和spring在数据库中添加一行。在一个maven项目中,我制作了一个观看视频的项目,因为我对spring和数据库还不熟悉Java namedParameterjdbcTemplate提供空指针异常,java,mysql,spring,jdbctemplate,Java,Mysql,Spring,Jdbctemplate,我一直在做一个简单的程序,使用jdbc和spring在数据库中添加一行。在一个maven项目中,我制作了一个观看视频的项目,因为我对spring和数据库还不熟悉 * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the e
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mariam.employee;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
/**
*
* @author shoaib kiani
*/
@Repository
public class EmployeeDaoImpl implements EmployeeDao{
NamedParameterJdbcTemplate namedPJT;
JdbcTemplate jt;
public SqlParameterSource getSqlP(Employee e)
{
MapSqlParameterSource m=new MapSqlParameterSource();
m.addValue("id", e.getID());
m.addValue("FirstName",e.getFirstName());
m.addValue("LastName",e.getLastName());
return m;}
@Override
public void save(Employee e) {
final String sql="INSERT INTO authors('FirstName','LastName') VALUE(?,? )";
namedPJT.update(sql , getSqlP(e));
System.out.println("Passed");
}
}
这是一个例外:
Exception in thread "main" java.lang.NullPointerException
at com.mariam.employee.EmployeeDaoImpl.save(EmployeeDaoImpl.java:38)
at com.mariam.employee.Main.main(Main.java:22)
更改这些行:
NamedParameterJdbcTemplate namedPJT;
JdbcTemplate jt;
为此:
@Autowired
NamedParameterJdbcTemplate namedPJT;
@Autowired
JdbcTemplate jt;
namedPJT
为null
。请阅读-总结是,这不是向志愿者讲话的理想方式,可能会对获得答案产生反作用。请不要将此添加到您的问题中。