0

I have been making a simple a program to add a row in the data base using jdbc and spring in a maven project I have made the project watching videos as I am new to spring and database.

* 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");



}

}

This is the exception:

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)
Sweet_Cherry
  • 313
  • 1
  • 4
  • 13
  • ``namedPJT`` is ``null``. – f1sh May 26 '18 at 21:58
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer May 26 '18 at 22:08

1 Answers1

1

Change these lines:

NamedParameterJdbcTemplate namedPJT;
JdbcTemplate jt;

To this:

@Autowired
NamedParameterJdbcTemplate namedPJT;
@Autowired
JdbcTemplate jt;
Jan B.
  • 6,030
  • 5
  • 32
  • 53
Farvardin
  • 5,336
  • 5
  • 33
  • 54