7
回答
您好,我想请教你java.lang.NullPointerException的问题,可以吗?感激不尽,拜托了
华为云实践训练营,热门技术免费实践!>>>   
出现的问题是:

严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at com.ning.database.DBcontrol.checkUserName(DBcontrol.java:72)
    at org.apache.jsp.regcheck_jsp._jspService(regcheck_jsp.java:95)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    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:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)


DBcontrol.java 代码
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBcontrol
{
	public boolean saveUser(User user)
	{
		Connection con = null;
		try
		{
			con = DBconn.getConn();
			String sql = "insert into user_imfor(u_name,u_pw,u_email,u_sex,u_age) values(?,?,?,?,?)";
			PreparedStatement stmt = con.prepareStatement(sql);
			stmt.setString(1,user.getName());
			stmt.setString(2,user.getPwd());
			stmt.setString(3,user.getEmail());
			stmt.setString(4,user.getSex());
			stmt.setInt(5,user.getAge());
			int count= stmt.executeUpdate();
			if(count ==1)
			{
				return true;
			}
		}
		catch (Exception e)
		{  
			e.printStackTrace();
		}
		finally
		{
			try
			{
				con.close();
			}
			catch (SQLException e)
			{
				
				e.printStackTrace();
			}
		}
		return false;
	}
	
	public boolean checkUserName(String name)
	{
		Connection con = null;
		try
		{
			con = DBconn.getConn();
			String sql = "select * from user_imfor where u_name =? ";
			PreparedStatement stmt = con.prepareStatement(sql);
			stmt.setString(1,name);
			ResultSet rs = stmt.executeQuery();
			if(rs.next())
			{
				return true;
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();

		}
		finally{
			try
			{
				con.close();
			}
			catch (SQLException e)
			{
				System.out.println("查询数据时出错" + e.toString());

			}
		}
		return false;
	}
}
<无标签>
举报
啊宁哥
发帖于5年前 7回/1K+阅
共有7个答案 最后回答: 5年前
finally语句块里 con.close()前先判断下con是否为null。
--- 共有 2 条评论 ---
啊宁哥是在try那里加上if(con!=null)吗?如果是这样也不行 5年前 回复
啊宁哥是在try那里加上if(con!=null)是吗?如果是这样也不行啊 5年前 回复

con = DBconn.getConn();

根源是这句,确认一下 是否由可能这个getConn()方法可能返回null或者抛出异常,特别是在内部捕获了异常造成返回为null的情况

另外finally那里要写成

if(con != null) {

    con.close();

    con = null;

}

--- 共有 2 条评论 ---
逝水fox回复 @啊宁哥 : 我又没有你的DBConn的代码,怎么知道具体是怎样的。只知道这个方法返回的链接应该是null,至于什么原因没法猜。 5年前 回复
啊宁哥具体应该是怎样? 5年前 回复
org.apache.jasper.JasperException: java.lang.NullPointerException
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.lang.NullPointerException
	com.ning.database.DBcontrol.saveUser(DBcontrol.java:38)
	org.apache.jsp.regcheck_jsp._jspService(regcheck_jsp.java:108)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

DBconn.java

 

import java.sql.Connection;
import java.sql.DriverManager;
public class DBconn
{
 private static Connection conn = null;
 public static Connection getConn()
 {
  try{

    Class.forName("com.mysql.jdbc.driver");
    String url = "jdbc:mysql://localhost/tourtime?user=root&password=zyn&useUnicode=true&characterEncoding=utf-8";
    conn = DriverManager.getConnection(url);

  
  }catch (Exception e) {

   e.printStackTrace();
  }
  return conn;
 }
}

顶部