hi guys, I'm writing some servlets for a website, but I'm new to JSP, therefore I made alot mistakes when I was writing them, I've been fixing them for weeks, but this null pointer exception, I just don't know where the problem is, and what caused it..... so here is the code, thanks alot guys!
package Servlet; import javax.servlet.*; import javax.servlet.http.*; import Model.DAO; import java.io.*; import java.sql.*; public class LoginServlet extends HttpServlet { Connection conn=null; Statement stmt=null; String username; String password; public void init(ServletConfig config)throws ServletException { super.init(); } public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException { DAO.getConnection(conn); try { stmt=conn.createStatement(); } catch (SQLException e1) { } username=request.getParameter("username"); password=request.getParameter("password"); if(username=="admin") { try { ResultSet rs=stmt.executeQuery("select '"+username+"' from tb_user"); if(rs.next()) { ResultSet vpass=stmt.executeQuery("select '"+password+"' from tb_user where id = '"+username+"'"); if(vpass.next()) { request.getRequestDispatcher("./manage/manage_index.jsp").forward(request,response); } else { request.getRequestDispatcher("http://localhost:8080/form_apply_system/account/log_fail.jsp").forward(request,response); } } }catch(Exception e) { e.printStackTrace(); } } else { try { ResultSet rs=stmt.executeQuery("select '"+username+"' from tb_user"); if(rs.next()==true) { ResultSet vpass=stmt.executeQuery("select '"+password+"' from tb_user where id = '"+username+"'"); if(vpass.next()==true) { request.getRequestDispatcher("http://localhost:8080/form_apply_system/account/log_success.jsp").forward(request,response); } else { request.getRequestDispatcher("http://localhost:8080/form_apply_system/account/log_fail.jsp").forward(request,response); } } else { request.getRequestDispatcher("http://localhost:8080/form_apply_system/account/log_fail.jsp").forward(request,response); } }catch(Exception e) { e.printStackTrace(); } } } }
I know this servlet sucks, so please just ignore its poor design and hard codind.....
--- Update ---
eclipse says that the null pointer exception occured at line 28, which is
stmt=conn.createStatement();
and DAO.getConnection is just a normal method used to get connection with MySQL database, here is the code of it:
[hightlight=Java]
package Model;
import java.sql.*;
public class DAO
{
public static void getConnection(Connection conn)
{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/web";
try
{
Class.forName(driver);
conn=DriverManager.getConnection(url,"root","12345 6");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
[/highlight]