Click here to Skip to main content
15,887,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi guys,
i have a problem in connecting my web application in javaEE, when i run the servlet i have an error : java.lang.NullPointerException

and this is a screenshot for the error : http://im51.gulfup.com/TnfWrg.jpg[^]

this the code where i try to connect to the database

Java
public class login_servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    public login_servlet() {
        super();
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        //this.getServletContext().getRequestDispatcher( "/login.jsp" ).forward( request, response );
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        java.sql.Connection connexion = null;
        java.sql.Statement statement = null;
        ResultSet resultat = null;
        String userQuery="";
 
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
        }
 
        try {
            connexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/Dlala", "IPLS", "password");
        } catch (SQLException e) {
            e.printStackTrace();
        }
 
        try {
            statement = connexion.createStatement();
 
            // Exécution de la requête
            resultat = statement.executeQuery("SELECT user FROM Client;");
 
            // Récupération des données
            while (resultat.next()) {
                userQuery = resultat.getString("user");
            }
        } catch (SQLException e) {
        } finally {
            // Fermeture de la connexion
            try {
                if (resultat != null)
                    resultat.close();
                if (statement != null)
                    statement.close();
                if (connexion != null)
                    connexion.close();
            } catch (SQLException ignore) {
            }
        }
        //************************************************//
 
        response.setContentType("text/html");   
        PrintWriter out = response.getWriter();   
 
        String n=request.getParameter("user");   
        String p=request.getParameter("pass");  
 
        HttpSession session = request.getSession(false); 
        if(session!=null) 
        session.setAttribute("name", n); 
 
        if( n.equals(userQuery)){   
            RequestDispatcher rd=request.getRequestDispatcher("home.jsp");   
            rd.forward(request,response);   
        }   
        else{  
            out.print("<p style=\"color:red\">Sorry username or password error</p>");   
            RequestDispatcher rd=request.getRequestDispatcher("login.jsp");   
            rd.include(request,response);   
        }
 
        out.close(); 
    }
}


the error is in this line : statement = connexion.createStatement();

please i need help
Posted
Updated 29-Dec-14 3:06am
v2
Comments
ridoy 29-Dec-14 7:27am    
what is the error message?
Imad_dz 29-Dec-14 9:05am    
this is a screenshot for the error im51.gulfup.com/TnfWrg.jpg

the 45th line is : statement = connexion.createStatement();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900