namedParameterjdbcTemplate给出nullpointer异常

时间:2018-05-26 21:56:20

标签: 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 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)

1 个答案:

答案 0 :(得分:0)

更改这些行:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(@"<html><body><p><table id=""foo""><tr><th>hello</th></tr><tr><td>world</td></tr></table></body></html>");
foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table")) {
Console.WriteLine("Found: " + table.Id);
foreach (HtmlNode row in table.SelectNodes("tr")) {
    Console.WriteLine("row");
    foreach (HtmlNode cell in row.SelectNodes("th|td")) {
        Console.WriteLine("cell: " + cell.InnerText);
    }
}
}

对此:

var query = from table in doc.DocumentNode.SelectNodes("//table").Cast<HtmlNode>()
        from row in table.SelectNodes("tr").Cast<HtmlNode>()
        from cell in row.SelectNodes("th|td").Cast<HtmlNode>()
        select new {Table = table.Id, CellText = cell.InnerText};

foreach(var cell in query) {
Console.WriteLine("{0}: {1}", cell.Table, cell.CellText);
}