Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to import table contents from a ms access table to a jsp web page table , however I keep getting an error on line 31 although my code is fine, can someone elaborate me on whats wrong here?

heres the jsp code :


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page language="java" import="java.io.*, java.util.*,java.sql.*"%>

<jsp:useBean id="data" scope="page" class="HW.search"/>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Web Tech Homework</title>
    </head>
    <body>
        <table border="1">
            <tr>
                <th></th>
                <th>CarID</th>
                <th>FirmName</th>
                <th>CarModel</th>
                <th>CarType</th>
                <th>ProductYear</th>
                <th>Km</th>     
                <th>Color</th>     
                <th>EngineType</th>     
                <th>Price</th>     
                <th>ExtraInformation</th>
            </tr>
            <%
                int counter = 0;
                ResultSet rs = data.getCarTable();
                while (rs.next()) { // row by row

                    //database colums name
                    counter++;
                    int t1 = rs.getInt("CarID"); // 1st col
                    String t2 = rs.getString("FirmName");// 1st col
                    String t3 = rs.getString("CarModel");// 1st col
                    String t4 = rs.getString("CarType");// 1st col
                    String t5 = rs.getString("ProductYear");// 1st col
                    String t6 = rs.getString("Km");// 1st col
                    String t7 = rs.getString("Color");// 1st col
                    String t8 = rs.getString("EngineType");// 1st col
                    String t9 = rs.getString("Price");// 1st col
                    String t10 = rs.getString("ExtraInfomation");// 1st col              
            %>

            <tr>
                <td><input type="radio" name="<%=counter%>"/></td>
                <td><%=t1%></td> 
                <td><%=t2%></td> 
                <td><%=t3%></td>             
                <td><%=t4%></td> 
                <td><%=t5%></td>            
                <td><%=t6%></td>            
                <td><%=t7%></td> 
                <td><%=t8%></td> 
                <td><%=t9%></td> 
                <td><%=t10%></td> 
            </tr>

            <% } //close while loop here %>  

        </table>
        <%
            rs.close();
        %>
    </body>
</html>


My Java Class For Establishing Access Connection :

package HW;

import java.sql.*;

public class database {

    static Connection con = null;

    public database() {
        makeConnection();
    }

    public void makeConnection() {
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String url = "jdbc:odbc:DB";
        try {
            //Class.forName(driver);
            con = DriverManager.getConnection(url);

        } catch (Exception err) {
            System.out.println(err);

        }
    }
}



My Java Class For Getting Table Contents :

package HW;

import java.sql.*;

public class search {

    public search() {
        new database();

    }

    public ResultSet getCarTable() {
        ResultSet rs = null;

        try {
            Statement stmt = database.con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM CAR");

        } catch (Exception err) {
            err.printStackTrace();
        }

        return rs;

    }

}


ERROR BY THE BROWSER :

HTTP Status 500 - java.lang.NullPointerException

type Exception report

message java.lang.NullPointerException

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:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
	HW.search.getCarTable(search.java:16)
	org.apache.jsp.index_jsp._jspService(index_jsp.java:101)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	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:727)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.53 logs.

Apache Tomcat/7.0.53
Posted
Updated 25-May-14 8:18am
v2
Comments
[no name] 25-May-14 10:43am    
What does "an error" mean? What is line 31?
M.Rahmat 25-May-14 14:18pm    
sorry,added the error

1 solution

Connection object getting null.
Java
Statement stmt = database.con.createStatement();


Check makeConnection() method whether it actually return an object or else.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900