In a simple servlet code,
I get a NULLPointerException thrown while giving query to prepared statement.
By what i understand, null pointer exception is thrown when an object defined null is given some new value.
But am not sure how to change the following code to run properly.
import java.sql.*;
public class DataLogic {
static Connection con;
static PreparedStatement ps;
static ResultSet rs;
DataLogic() {
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","");
} catch(Exception e){
e.printStackTrace();
}
}
public static boolean login(String user, String pass) {
boolean b=false;
try{
/*Exception thrown in next line*/
ps=con.prepareStatement("select * from login where uname=? and pass like ?");
ps.setString(1, user);
ps.setString(2, pass);
rs=ps.executeQuery();
//System.out.print(b=rs.next());
} catch(Exception e){
e.printStackTrace();
}
return b;
}
}
PS: This is invoked from a servlet.
Update:
java.lang.NullPointerException at data.DataLogic.login(DataLogic.java:22) at serv.login.doPost(login.java:35) at serv.login.doGet(login.java:26) 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:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Blockquote
Pls help. Thnks in advance.