【问题标题】:namedParameterjdbcTemplate giving nullpointer exception [duplicate]namedParameterjdbcTemplate 给出空指针异常[重复]
【发布时间】:2018-11-05 22:08:50
【问题描述】:

我一直在制作一个简单的程序,在一个 maven 项目中使用 jdbc 和 spring 在数据库中添加一行我已经制作了这个项目来观看视频,因为我是 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 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)

【问题讨论】:

标签: java mysql spring jdbctemplate


【解决方案1】:

更改这些行:

NamedParameterJdbcTemplate namedPJT;
JdbcTemplate jt;

到这里:

@Autowired
NamedParameterJdbcTemplate namedPJT;
@Autowired
JdbcTemplate jt;

【讨论】:

    猜你喜欢
    • 2014-09-06
    • 2014-07-19
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多