0

我正在尝试此代码,以便了解如何将 MS SQL 2005 连接到 Eclipse 和 Tomcat 7 作为我的服务器。我是制作 Java Web 应用程序的新手,所以请多多包涵。在下面的错误中,我如何追踪错误的原因在哪里?该项目有2个jsp和3个类文件。index.jsp, result.jsp,和. SQLConnection.java_Items.javaItemController.java

错误是这样的,

type Exception report

message 

description The server encountered an internal error () that prevented it from    fulfilling this request.

exception 

org.apache.jasper.JasperException: java.lang.NullPointerException
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause 

java.lang.NullPointerException
connection.SQLConnection.execSelectQuery(SQLConnection.java:35)
model.Item.lastItems(Item.java:10)
org.apache.jsp.index_jsp._jspService(index_jsp.java:83)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

SQLConnection.java 和 index.jsp 的代码如下;

SQLConnection.java

    package connection;
    import java.sql.*;
    public class SQLConnection {
    protected Connection con=null;
    protected ResultSet rs=null;

    public SQLConnection(String address)
    {
        try
    {
    String conString = "jdbc:sqlserver://localhost:1433;databaseName=Chingdb;    integratedSecurity=true;";
con = DriverManager.getConnection(conString);
}catch(SQLException sqlError){
//ERROR MANAGEMENT HERE
}
}
public boolean execQuery(String sql)
{
boolean result = false;
try
{
Statement stmt = con.createStatement();
stmt.execute(sql);
result = true;
}catch(SQLException sqlError){
//ERROR MANAGEMENT HERE
result = false;
}
return result;
}
public void execSelectQuery(String sql)
{
try
{
Statement stmt = con.createStatement();
this.rs = stmt.executeQuery(sql);
}catch(SQLException sqlError){
//ERROR MANAGEMENT HERE
}
}
public void disconnect()
{
try
{
con.close();
}catch(SQLException sqlError){
//ERROR MANAGEMENT HERE
}

}
}

索引.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org    /TR/html4/loose.dtd">
<%@page import="model.Item"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Todo-List App</title>
</head>
<body>
<h2>Add a new item</h2>
<form action="addItem" method="post">
<table>
<tr>
<td><input type="text" value="I have to..." onClick="this.value('')" name="todo"/></td>
<td><input type="submit" value="Send" name="send"/> </td>
</tr>
</table>
</form>
<h2>Todo-List</h2>
<%
Item item = new Item();
String[] lastest = item.lastItems();
for(int i=0;i<lastest.length;i++)
{%>
<%=lastest[i] %> <br>
<%}item.disconnect();
%>
</body>
</html>
4

1 回答 1

0

错误堆栈跟踪表明您正在进入NullPointerException的方法SQLConnection.javaexecSelectQuery并且当您从 调用此方法时遇到此异常 Item.java

java.lang.NullPointerException
connection.SQLConnection.execSelectQuery(SQLConnection.java:35)

将调试点放置Item.java在 中model.Item.lastItems(Item.java:10),然后放置在Statement stmt = con.createStatement();

我怀疑你Connection可能是NULL。

于 2012-10-10T04:28:01.157 回答