I am trying this code in order for me to understand how to connect MS SQL 2005 to Eclipse and Tomcat 7 as my server. I am just new in making a java web application so please bear with me. In the error below, how can i trace where is the cause of the error? The project have 2 jsp, and 3 class files. index.jsp, result.jsp, SQLConnection.java, Items.java and ItemController.java.
The error goes like this,
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)
The code for SQLConnection.java and index.jsp are as follows;
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
}
}
}
index.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>
The error stack trace says that you are getting
NullPointerExceptioninSQLConnection.java‘sexecSelectQuerymethod and you are getting this exception when you are calling this method fromItem.java.Place a debug point at
Item.javain themodel.Item.lastItems(Item.java:10)and thenat
Statement stmt = con.createStatement();I doubt your
Connectionmight be NULL.