-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;
    }
marouu
  • 17
  • 5
  • Please replace all `e.printStackTrace();` by `throw new RuntimeException(e);`. This way you code will fail much sooner with a much clearer cause. Right now you're basically ignoring all of these `e.printStackTrace()` and only paying attention to the `NullPointerException` which is merely a consequence thereof. – BalusC Nov 27 '19 at 11:20

1 Answers1

0

The error says the null pointer is in your Dao, and controller.

Try setting break points around each of the line numbers in each file to see what is happening in debug mode.

It should give you a good idea of what's going wrong.

Also are you setting the result set data to object variables??

user2963022
  • 165
  • 3
  • 16