日期:2014-05-17  浏览次数:20825 次

急急急,看看为什么会抛空异常呢?
代码:
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

public class InitServlet extends HttpServlet {
private String driver="";
private String URL="";
private String username="";
private String password="";
public void destroy() {
super.destroy();  


// Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
Connection con = this.getConnection();
try{
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from user");
while(rs.next()){
out.print(rs.getString("u_id"));
out.println();
out.print(rs.getString("u_name"));
out.print(rs.getString("u_password"));

}
}catch(SQLException e){
e.printStackTrace();
}

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}

public void init() throws ServletException {
driver=getInitParameter("driver");
URL=getInitParameter("URL");
username=getInitParameter("username");
password=getInitParameter("password");
}
public Connection getConnection(){
Connection con=null;
try{
Class.forName(driver);
con = DriverManager.getConnection(URL,username,password);
System.out.print("2");

}catch(Exception e){

}
return con;
}

}

错误信息:
严重: Servlet.service() for servlet InitServlet threw exception
java.lang.NullPointerException
at ch02.InitServlet.doGet(InitServlet.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)