-1

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) {
marouu
  • 17
  • 5
  • Can you show us your `SingletonConnection` class? – Imaguest Nov 26 '19 at 10:15
  • this is the line 48 ::PreparedStatement ps=connection.prepareStatement("SELECT * FROM PRODUITS WHERE DESIGNATION LIKE ?"); – marouu Nov 26 '19 at 10:15
  • 1
    In your question you are saying `connection` is `null` as that is what is printed out. So that means `SingletonConnection.getConnection()` returns `null` as you are assigning the result to `connection`. So look in `SingletonConnection.getConnection()` next! – DanielBarbarian Nov 26 '19 at 10:18
  • ok i will add it – marouu Nov 26 '19 at 10:18
  • @ DanielBarbarian i have added my singletonconnection please if you can check it – marouu Nov 26 '19 at 10:21
  • Using a singleton connection in a Java EE application is a bad idea. Under concurrent access, you'll get problems with partial commits, incorrect rollbacks, statements or result sets magically closing, etc. Please learn about connection pools and use them. – Mark Rotteveel Nov 26 '19 at 13:54
  • @ Mark Rotteveel y're alright , but i'm a beginner i must learn the singleton connection, – marouu Nov 26 '19 at 14:03
  • Please do not ignore the `e.printStackTrace()` lines in server log. Your answer is right there. Better yet, replace all these useless lines by `throw new RuntimeException(e)`. This way your code will correctly die much sooner with a much clearer message. – BalusC Nov 27 '19 at 11:21
  • @ BalusC i added the stack in server log please check it – marouu Nov 27 '19 at 18:42

2 Answers2

0

Do you add mysql-connector-java to the project build path

TiantianHe
  • 48
  • 1
  • 6
0

@marouu I think the NullPointerException is not caused by Connection,maybe the ProduitDaoImpl,Do you add @Service in this class

TiantianHe
  • 48
  • 1
  • 6