空指针异常Java Eclipse NEON

时间:2017-05-10 10:40:47

标签: java eclipse servlets

处理requestContext Path:/ Employees

时出错

Servlet路径:/ Dipendente

路径信息:null

查询字符串:null

堆栈跟踪

java.lang.NullPointerException
it.proxima.dipendenti.dao.DipendenteDAO.getDipendente(DipendenteDAO.java:53)

这是DAO

 public class DipendenteDAO 
{
private Connection con;
private Statement cmd;
private static DipendenteDAO istance;

public static DipendenteDAO getIstance()
{
    if(istance == null) istance = new DipendenteDAO();
    return istance;
}

private DipendenteDAO()
{
    try 
    {   
        DataSource ds = (DataSource) new 
        InitialContext().lookup("java:jboss/datasources/andreadb");

        Connection con = ds.getConnection();
        System.out.println("con:"+con);

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

public Dipendente getDipendente(String Codicefiscale)
{
    Dipendente result = null;
    try
    {
        // Eseguiamo una query e immagazziniamone i risultati in un oggetto 
        ResultSet   
        String qry = "SELECT * FROM dipendenti WHERE Codicefiscale 
        ='"+Codicefiscale+"'";
        ResultSet res = cmd.executeQuery(qry);

        while(res.next())
        {
            result = new Dipendente();
            result.setNome(res.getString("Nome"));
            result.setCodicefiscale("Codicefiscale");
            result.setCognome(res.getString("Cognome"));
            result.setDatadinascita(res.getDate("Datadinascita"));
            result.setLuogodinascita(res.getString("Luogodinascita"));
        }
    } 
    catch (SQLException e)
    {
        // Auto-generated catch block
        e.printStackTrace();
    }
    return result;

}

这是Servlet代码

@WebServlet("/Dipendente")
public class DipendenteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;



public Dipendente getAnagrafica(String Cf)
{
    DipendenteDAO dd = DipendenteDAO.getIstance();
    Dipendente dip = dd.getDipendente(Cf);
    if(dip == null) System.out.println("ERRORE: Codice fiscale non presente 
    nel sistema");
    else System.out.println(dip.getNome() + " " + dip.getCognome());
    dd.close();
    return dip;
}
protected void doGet(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {

    String codiceFiscale = request.getParameter("Codice_Fiscale");
    Dipendente ds = this.getAnagrafica(codiceFiscale);

    response.getWriter().append(ds.getNome()+" "+ds.getCognome());



}

有谁知道可能依赖什么?

错误的第53行是:

53: ResultSet res = cmd.executeQuery(qry);

在调试模式下,语句cmd为null

初始化cmd我得到了同样的错误。也许连接中出现了另一个错误?

2 个答案:

答案 0 :(得分:2)

您的cmd是......

private Statement cmd;

...但你永远不会给它任何东西。那么你怎么期望它是null之外的其他东西呢?尝试在null对象上调用方法将产生NullPointerException

在第53行之前你缺少的是这样的......

cmd = con.createStatement();

这会让Connection对象创建一个新的Statement并将其分配给您的cmd变量。

另请注意这个......

Connection con = ds.getConnection();

...意味着......

private Connection con;

...也将始终null,导致同样的问题。将其替换为......

con = ds.getConnection();

说明:使用您的代码,您创建的新变量con仅在构造函数中有效,而另一个名为con的变量仍为null 。这不是你想要的。您想以某种方式创建Connection并将其分配给已存在的变量con,而不是创建一个新的变量con,一旦构造函数完成就会被遗忘。

答案 1 :(得分:0)

使用以下语句初始化Statement:

cmd = con.createStatement();

之前

ResultSet res = cmd.executeQuery(qry);

https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html