0

I am trying to create a web app that is querying my oracle database and returns a list of menu titles from a table but when i navigate to the page it throws the area shown below.

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: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
oracle.DatabaseConnector.selectAllMenus(DatabaseConnector.java:63)
org.apache.jsp.menus_jsp._jspService(menus_jsp.java:113)
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)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.35 logs.

Below is the offending part of the code inside my java class that queries the database. But i am not sure why this would cause the above error? Can someone take a look and see if there is a problem?

public MenuList selectAllMenus(String ordering) {
    MenuList list = null;

    try {
        String strQuery = "SELECT title"
                + " FROM menus"
                + " ORDER BY " + ordering;
        PreparedStatement stmt = conn.prepareStatement(strQuery);

        results = stmt.executeQuery();

        list = new MenuList(results);
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return list;
}

This is what was in the log file:

28-Feb-2012 14:40:57 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at oracle.DatabaseConnector.selectAllMenus(DatabaseConnector.java:63)
at org.apache.jsp.menus_jsp._jspService(menus_jsp.java:113)
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.Http11AprProcessor.process(Http11AprProcessor.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
at java.lang.Thread.run(Thread.java:662)
5
  • 1
    which line is exactly at the DatabaseConnector.java:63? Commented Feb 28, 2012 at 14:41
  • The line in question is PreparedStatement stmt = conn.prepareStatement(strQuery); Commented Feb 28, 2012 at 14:45
  • Ahh.. then as @vulkanio said, it must be your conn which is null at this point. Also as he said, put a catch block for Exception e and do a e.printStackTrace() Would help to understand the error more correctly. Commented Feb 28, 2012 at 14:49
  • 2
    I'd suggest to go through a bit decent JDBC tutorial. The way how you're managing DB resources is wrong. Commented Feb 28, 2012 at 14:51
  • Also add results.close(); and stmt.close() and ResultSet results = ...;. @BalusC meant it well. Ensure that the MenuList constructor copies the values immediately. Commented Feb 28, 2012 at 20:45

4 Answers 4

2

Hope you are using MySQL DB..

Make sure you add the correct MySQL Java Connector as an external library and also add Tomcat as an external server (In the Build Path of the Project in Eclipse).

(You can also copy all the libraries in the lib folder of your $TOMCAT_HOME/$CATALINA_HOME to your build path.

This should resolve this problem.

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

1

You can look in your logs : Apache Tomcat/6.0.35 logs

But the error message itself is pretty clear : The db connection is null, probably your connection string/parameters are incorrect.

Comments

0

I assume conn is open.

Instead of printing the stack trace, why don't you print the error message?

1 Comment

printStackTrace does include the message. System-generated NullPointerExceptions do not have messages — the first stack trace line number is your source of information.
0

line:

PreparedStatement stmt = conn.prepareStatement(strQuery);

"conn" is not being created in any place

try something like this to create a connection:

public class MyConnection {
Connection conn;
String username;
String password;
public Connection getConnection(){
    try {
        String url = "jdbc:mysql://localhost/yourDBName?";
        Class.forName("com.mysql.jdbc.Driver");
        try {
            conn = (Connection) DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
        System.out.println(e);
        }

    } catch (SQLException e) {
        System.out.println(e);
    }
    return conn;
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.