I'm trying to implement a singleton DB connection in Java. I think I have it mostly correct, but when I try to use it in other classes, I keep getting java.lang.NullPointerExceptions.
The error I'm getting is in my ProduitDaoImpl class, on the PreparedStatement line where I do connection.prepareStatement(query); I think connection is being passed as null, but I don't know why or how to fix it.
here is my ProduitDaoImpl
public class ProduitDaoImpl implements IProduitDao {
    public List<Produit> produitsParMC(String mc) {
        List<Produit> produits=new ArrayList<Produit>();
        Connection connection=SingletonConnection.getConnection();
        try {
            PreparedStatement ps=connection.prepareStatement("SELECT * FROM PRODUITS WHERE DESIGNATION LIKE ?");
            ps.setString(1, mc);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                Produit p=new Produit();
                p.setId(rs.getLong("ID"));
                p.setDesignation(rs.getString("DESIGNATION"));
                p.setPrix(rs.getDouble("Prix"));
                p.setQuantite(rs.getInt("QUANTITE"));
                produits.add(p);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return produits;
    }
}
and here is my singleton
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;
    }
}
 
    