I'm beginner in JEE and i'm trying to fetch object from my db but i has encountered an error of nullpointerexception, i tried to locate the origin of the nullpointerexception(i understood that the connection passed as null) by System.out.println() but i failed, i forgot to tell you that i have class singletonconnection for connection and i call it in my function by .getconnection() also i add a jdbc driver (version 8.0.18) to the project build path(i ask if this error can be due to the jdbc driver version??),,
please if someone can help and thanks in advance.
here is the function that connects to the db and brings objects where i have the error that i told you about in line PreparedStatement ps=connection.prepareStatement("req")
System.out.println("connection");
Connection connection=SingletonConnection.getConnection();
System.out.println(connection);
try {
PreparedStatement ps=connection.prepareStatement("SELECT * FROM PRODUITS WHERE DESIGNATION LIKE ?");
ps.setString(1, mc);
ResultSet rs=ps.executeQuery();
here is the error log
HTTP Status 500 – Internal Server Error
Type Exception Report
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.NullPointerException
dao.ProduitDaoImpl.produitsParMC(ProduitDaoImpl.java:48)
web.ControleurServelet.doGet(ControleurServelet.java:49)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.
here is my singleton connection
public class SingletonConnection {
private static Connection connection;
//le block static charge la classe en mémoire lors de son appel
static{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
try {
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db-natal","root","");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
return connection;
}
here is the stack in the server log
Nov 27, 2019 6:26:37 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [cs] in context with path [/essai_web] threw exception
java.lang.NullPointerException
at dao.ProduitDaoImpl.produitsParMC(ProduitDaoImpl.java:48)
at web.ControleurServelet.doGet(ControleurServelet.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:444)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1137)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
here is the line 48
public Produit update(Produit p) {