我正在尝试创建一个查询我的oracle数据库的Web应用程序,并从表中返回一个菜单标题列表,但是当我导航到该页面时,它会抛出下面显示的区域。
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.
以下是我的java类中查询数据库的代码中令人讨厌的部分。但我不确定为什么会导致上述错误?有人可以看看是否有问题吗?
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;
}
这是日志文件中的内容:
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)
答案 0 :(得分:2)
希望您使用的是MySQL DB ..
确保将正确的MySQL Java Connector添加为外部库,并将Tomcat添加为外部服务器(在Eclipse的项目构建路径中)。
(您还可以将$ TOMCAT_HOME / $ CATALINA_HOME的lib文件夹中的所有库复制到构建路径。
这应解决此问题。
感谢。
答案 1 :(得分:1)
您可以查看日志:Apache Tomcat/6.0.35 logs
但错误消息本身非常清楚:数据库连接为空,可能是您的连接字符串/参数不正确。
答案 2 :(得分:0)
我认为conn
已打开。
为什么不打印错误消息,而不是打印堆栈跟踪?
答案 3 :(得分:0)
行:
PreparedStatement stmt = conn.prepareStatement(strQuery);
“conn”未在任何地方创建
尝试这样的方法来创建连接:
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;
}
}