簡體   English   中英

空指針異常Java Eclipse NEON

[英]Null Pointer Exception Java Eclipse NEON

處理requestContext時出錯,路徑:/ Employees

Servlet路徑:/ Dipendente

路徑信息:空

查詢字符串:空

堆棧跟蹤

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為空

初始化cmd我得到同樣的錯誤。 連接con可能還有另一個錯誤?

你的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();

說明:使用您的代碼,您正在創建一個NEW變量con ,該變量僅在構造函數中有效,而另一個變量也稱為con ,仍將為null 這不是您想要的。 您想以某種方式創建一個Connection並將其分配給已經存在的變量con ,而不是創建一個新的變量con ,該變量一旦構造函數完成便會被忘記。

用以下方法初始化語句:

cmd = con.createStatement();

之前

ResultSet res = cmd.executeQuery(qry);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM