主题:jsp中出现java.lang.NullPointerException怎么解决
package login;
import java.sql.*;
//import com.mircosoft.jdbc.sqlserver.SQLServerDriver;
public class UserRegist
{
private UserInfo userInfo;
private Connection con;
public UserRegist()
{
String CLASSFORNAME = "com.mircosoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspbook";
String USER = "sa";
String PWD = "123";
try
{
Class.forName(CLASSFORNAME).newInstance();
con = DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//设置待注册的用户信息
public void setUserInfo(UserInfo userInfo)
{
this.userInfo = userInfo;
}
//进行注册
public void regist() throws Exception
{
String reg = "insert into user_Info values(?,?,?,?,?,?,?,?,?,?,?)";
try
{
PreparedStatement pstmt = con.prepareStatement(reg);
//创建一个预处理语句,然后设置它们的参数
pstmt.setString(1,userInfo.getUserId());
pstmt.setString(2,userInfo.getName());
pstmt.setString(3,userInfo.getPhone());
pstmt.setString(4,userInfo.getCity());
pstmt.setString(5,userInfo.getEmail());
pstmt.setString(6,userInfo.getQuestion());
pstmt.setString(7,userInfo.getAnswer());
pstmt.setDate(8,new java.sql.Date(new java.util.Date().getTime()));
pstmt.setString(9,userInfo.getPassWord());
pstmt.setInt(10,1);
pstmt.setInt(11,0);
//执行操作
pstmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
}
错误如下:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
login.UserRegist.regist(UserRegist.java:41)
org.apache.jsp.newuser_jsp._jspService(newuser_jsp.java:72)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.
import java.sql.*;
//import com.mircosoft.jdbc.sqlserver.SQLServerDriver;
public class UserRegist
{
private UserInfo userInfo;
private Connection con;
public UserRegist()
{
String CLASSFORNAME = "com.mircosoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspbook";
String USER = "sa";
String PWD = "123";
try
{
Class.forName(CLASSFORNAME).newInstance();
con = DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//设置待注册的用户信息
public void setUserInfo(UserInfo userInfo)
{
this.userInfo = userInfo;
}
//进行注册
public void regist() throws Exception
{
String reg = "insert into user_Info values(?,?,?,?,?,?,?,?,?,?,?)";
try
{
PreparedStatement pstmt = con.prepareStatement(reg);
//创建一个预处理语句,然后设置它们的参数
pstmt.setString(1,userInfo.getUserId());
pstmt.setString(2,userInfo.getName());
pstmt.setString(3,userInfo.getPhone());
pstmt.setString(4,userInfo.getCity());
pstmt.setString(5,userInfo.getEmail());
pstmt.setString(6,userInfo.getQuestion());
pstmt.setString(7,userInfo.getAnswer());
pstmt.setDate(8,new java.sql.Date(new java.util.Date().getTime()));
pstmt.setString(9,userInfo.getPassWord());
pstmt.setInt(10,1);
pstmt.setInt(11,0);
//执行操作
pstmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
}
错误如下:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
login.UserRegist.regist(UserRegist.java:41)
org.apache.jsp.newuser_jsp._jspService(newuser_jsp.java:72)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.