Issue
Error processing requestContext Path:/Employees
Servlet Path:/Dipendente
Path Info:null
Query String:null
java.lang.NullPointerException
it.proxima.dipendenti.dao.DipendenteDAO.getDipendente(DipendenteDAO.java:53)
This is 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;
}
this is the Servlet code
@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());
}
does anyone know what might depend?
Line 53 of the error is this :
53: ResultSet res = cmd.executeQuery(qry);
in debug mode, the statement cmd is null
Initializing cmd i get the same error. Maybe there's another mistake in the connection con ?
Solution
Your cmd
is...
private Statement cmd;
...but you never assign anything to it. So how do you expect it to be anything else then null
? Trying to call a method on a null
object, will result in a NullPointerException
.
What you are missing is something like this before Line 53...
cmd = con.createStatement();
This will let the Connection
object create a new Statement
and assign that to your cmd
variable.
Also please note that this...
Connection con = ds.getConnection();
...means that the...
private Connection con;
...will also be always null
, resulting in the same problem. Replace it with...
con = ds.getConnection();
Explanation: With your code, you are creating a NEW variable con
, which is only valid in the constructor, while the other variable, also named con
, will still be null
. This is not what you want. You want to create a Connection
somehow and assign it to the already existing variable con
, not create a new variable con
that will be forgotten as soon as the constructor is done.
Answered By - Florian Schaetz
Answer Checked By - Timothy Miller (JavaFixing Admin)