java servlet - error HTTP 500 - java.lang.nullpointer exception
-
I'm working on a servelet project. I use database connection (jdbc odbc) when I try to access data into database, I get HTTP status 500 error java.lang.NullPointerException checkpps.doGet(checkpps.java:61) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) line 61 in my code is : statement = link.createStatement(); any body could help me please???
-
I'm working on a servelet project. I use database connection (jdbc odbc) when I try to access data into database, I get HTTP status 500 error java.lang.NullPointerException checkpps.doGet(checkpps.java:61) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) line 61 in my code is : statement = link.createStatement(); any body could help me please???
At the risk of stating the obvious, link is null. More than that I cannot say, because I don't know what link is or how you get hold of it.
-
At the risk of stating the obvious, link is null. More than that I cannot say, because I don't know what link is or how you get hold of it.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;public class checkpps extends HttpServlet{
private Statement statement;
private Connection link;
private String URL = "jdbc:odbc:quiz";
String name;public void init() throws ServletException
{super.init(); try{ //load & register drive Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // get a connection to database link = DriverManager.getConnection("jdbc:odbc:quiz","sara", "sara"); } //catch (Exception e) //{ //e.printStackTrace(); //System.exit(1); //} catch (ClassNotFoundException e1){ System.out.println("driver not found"); } catch (SQLException e2){ System.out.println("error in connection"); // handle error getting connection} } //finally{ //try{ // close the connection to release db resources //if (link!=null) link.close(); // conn closed? //} //catch (SQLException e3){ } // ignored
//}
}public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType ("text/html"); PrintWriter out= res.getWriter(); String city = req.getParameter("city"); String ppsnum = req.getParameter("ppsno"); try { statement = link.createStatement(); ResultSet result = statement.executeQuery("SELECT \* FROM player"); //statement.close(); //String pps = result.getString("ppsno"); //if (!result.wasNull()) { //res.sendRedirect("youPlayed"); //} //}//end try while(result.next()) { out.println("<p>" + result.getString("ppsno")); } } catch (SQLException e) { System.out.println("error statement"); } } public void destroy() { try { link.close(); } catch (Exception e) { System.out.println("error on closing!!"); e.printStackTrace(); System.exit(1); } } }
as you see in my code, I'm trying to connect to database (MS Access) and link is my connection name. seems my connection has problem for some reason but still couldn't solve it. I have created DSN by the way. any help?
modified on Wednesday, November 11, 2009 8:51 AM
-
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;public class checkpps extends HttpServlet{
private Statement statement;
private Connection link;
private String URL = "jdbc:odbc:quiz";
String name;public void init() throws ServletException
{super.init(); try{ //load & register drive Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // get a connection to database link = DriverManager.getConnection("jdbc:odbc:quiz","sara", "sara"); } //catch (Exception e) //{ //e.printStackTrace(); //System.exit(1); //} catch (ClassNotFoundException e1){ System.out.println("driver not found"); } catch (SQLException e2){ System.out.println("error in connection"); // handle error getting connection} } //finally{ //try{ // close the connection to release db resources //if (link!=null) link.close(); // conn closed? //} //catch (SQLException e3){ } // ignored
//}
}public void doGet ( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType ("text/html"); PrintWriter out= res.getWriter(); String city = req.getParameter("city"); String ppsnum = req.getParameter("ppsno"); try { statement = link.createStatement(); ResultSet result = statement.executeQuery("SELECT \* FROM player"); //statement.close(); //String pps = result.getString("ppsno"); //if (!result.wasNull()) { //res.sendRedirect("youPlayed"); //} //}//end try while(result.next()) { out.println("<p>" + result.getString("ppsno")); } } catch (SQLException e) { System.out.println("error statement"); } } public void destroy() { try { link.close(); } catch (Exception e) { System.out.println("error on closing!!"); e.printStackTrace(); System.exit(1); } } }
as you see in my code, I'm trying to connect to database (MS Access) and link is my connection name. seems my connection has problem for some reason but still couldn't solve it. I have created DSN by the way. any help?
modified on Wednesday, November 11, 2009 8:51 AM
Which MSOffice you are using. And r you sure that you have selected the correct driver type.