哪位大哥帮小弟看看java.lang.NullPointerException
package JavaBean;
import java.sql.*;
import java.io.PrintWriter;
public class DB_conn{
private Connection conn=null;
public Statement sm=null;
private PrintWriter out = null;
public void connectDB(){
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ").newInstance();
conn=DriverManager.getConnection( "jdbc:odbc:shop ", "sa ", " ");
sm=conn.createStatement();
}
catch(Exception e){
out.print( "数据库连接失败! ");
}
}
public void closeDB(){
try{
if(sm!=null)
{
sm.close();
}
conn.close();
}
catch(Exception e){
out.println( "数据库关闭失败! ");
}
}
}
package JavaBean;
public class user{
private int userid;
private String username;
private String pwd;
private String email;
private int gender;
private String strgender;
private String tel;
public int getuserid() {
return userid;
}
public void setuserid(int id) {
this.userid=id;
}
public String getusername() {
return username;
}
public void setusername(String username) {
this.username=username;
}
public String getpwd() {
return pwd;
}
public void setpwd(String pwd) {
this.pwd=pwd;
}
public String getemail() {
return email;
}
public void setemail(String email) {
this.email=email;
}
public int getgender() {
return gender;
}
public void setgender(int gender) {
this.gender=gender;
}
public String getstrgender(){
if (gender == 0)
strgender= "女 ";
else
strgender= "男 ";
return strgender;
}
public String gettel() {
return tel;
}
public void settel(String tel) {
this.tel=tel;
}
}
package JavaBean;
import java.io.*;
import java.sql.*;
import java.util.*;
import JavaBean.user;
import JavaBean.DB_conn;
public class DB_user
{
DB_conn conn=new DB_conn();
public void add(user us)
{
conn.connectDB();
try{
String sql= "insert into userinfo(user_name,pwd, "
+ "email,gender,tel) values( ' " + us.getusername()
+ " ', ' " + us.getpwd() + " ', ' " + us.getemail()
+ " ', ' " + us.getgender() + " ', ' " + us.gettel()
+ " ') ";
conn.sm.executeUpdate(sql);
}
catch(Exception e){
System.out.println( "执行失败! ");
}
finally{
conn.closeDB();
}
}
public void update(user us)
{
conn.connectDB();
try {
String sql = "update userinfo set user_name= ' "
+ us.getusername() + " ',pwd= ' "
+ us.getpwd() + " ',email= ' "
+ us.getemail() + " ',gender= ' "
+ us.getgender() + " ',tel= ' "
+ us.gettel() + " ' where user_id= ' "
+ us.getuserid() + " ' ";
conn.sm.executeUpdate(sql);
}
catch (Exception e) {
System.out.println( "执行失败! ");
}
finally {
conn.closeDB();
}
}
public user get(String username, String password)
{
conn.connectDB();
ResultSet rs = null;
try {
String sql = "select * from userinfo where user_name = ' "
+ username + " ' and pwd= ' " + password + " ' ";
rs = conn.sm.executeQuery(sql);
user us = null;
if (rs.next())
{
us = new user();
us.setuserid(rs.getInt( "user_id "));
us.setusername(rs.getString( "username "));
us.setpwd(rs.getString( "pwd "));
us.setemail(rs.getString( "email "));
us.setgender(rs.getInt( "gender "));
us.settel(rs.getString( "tel "));
return us;
}
return us;
}
catch (Exception e) {
System.out.println(e);
return null;
} finally {
conn.closeDB();
}
}
}
package Servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import JavaBean.user;
import JavaBean.DB_user;
public class LoginServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
public LoginServlet()
{
super();
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
response.setContentType( "text/html;charset=GB2312 ");
String username=request.getParameter( "logname ");
String password=request.getParameter( "logpwd ");
DB_user dbuser = new DB_user();
user us=dbuser.get(username,password);
if(us!=null)
{
request.getSession(true).setAttribute( "username ",username);
try{
request.getRequestDispatcher( "index.jsp ").forward(request,response);
}
catch(Exception e){
System.out.println(e);
}
}
else
{
request.setAttribute( "error ", "用户名与密码不匹配,请重试! ");
try{
request.getRequestDispatcher( "Login.jsp ").forward(request,response);
}
catch(Exception e){
System.out.println(e);
}
}
}
}
HTTP Status 500 -
--------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
JavaBean.DB_conn.connectDB(DB_conn.java:15)
JavaBean.DB_user.get(DB_user.java:54)
Servlet.LoginServlet.doPost(LoginServlet.java:20)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs.
--------------------------------------------
Apache Tomcat/5.5.23
[解决办法]
out.print( "数据库连接失败! ");
out.println( "数据库关闭失败! ");
直接用System.out.println( "XXXXXXXXXXXXXXX ");
你的out没有赋值 当然报错
[解决办法]
PrintWriter是抽象类吧,好像不能实例化,要实现后才能实例化
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html