namedparameterjdbctemplate出现nullpointer异常

pxq42qpu  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(432)

这个问题在这里已经有答案了

什么是nullpointerexception,如何修复它(12个答案)
两年前关门了。
我一直在做一个简单的程序,在数据库中添加一行使用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 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)
oxcyiej7

oxcyiej71#

更改这些行:

NamedParameterJdbcTemplate namedPJT;
JdbcTemplate jt;

对此:

@Autowired
NamedParameterJdbcTemplate namedPJT;
@Autowired
JdbcTemplate jt;

相关问题