这是我第一次尝试使用带有servlet(和Java本身)的Java Web应用程序。我正在尝试创建一个MySQL类,该类将处理查询,连接等。无论出于何种原因。我可以获得连接,但是一旦尝试执行查询,它就会给出一个
java.lang.NullPointerException
我可以肯定我犯了某种菜鸟错误。但我不清楚这是什么。
这是SQL类
package coreservlets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import com.mysql.jdbc.*;
public class DBInterface {
private static Connection conn;
private Statement statement;
public static Connection Conn() throws SQLException, ClassNotFoundException {
String url = "jdbc:mysql://localhost:3306/cmwebapp";
String user = "user";
String pw = "password";
try{
//Class.forName("com.mysql.jdbc.Driver").newInstance();
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(url, user, pw);
//System.out.println(conn.getAutoCommit());
} catch (SQLException e) {
System.out.println("MySQL Connection failed.");
e.printStackTrace();
}
return conn;
}
public ResultSet Query(String query){
try{
statement = (Statement) conn.createStatement();
ResultSet rs = statement.executeQuery(query);
System.out.println(rs);
return rs;
}catch(Exception e){
System.out.println("TestServlet.java SQL connection failed.");
e.printStackTrace();
return null;
}
}
}
我正在尝试调用的班级
package internalLogic;
import coreservlets.*;
import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TheWork {
private DBInterface conn;
private String param;
private ResultSet set;
public TheWork(String p){
try {
conn = (DBInterface) DBInterface.Conn();
param = "SELECT * FROM test WHERE index =" + p ;
} catch(Exception e) {
System.out.println("In TheWork ctor SQL connection or variable assignment failed.");
e.printStackTrace();
}
}
public String Execute(){
set = conn.Query(param);
System.out.println(set);
Object s = null;
String ret = "";
try{
while (set.next()) {
// Make container class for SQL result sets.
if(s instanceof String){
s = set.getObject("index");
}
if (s instanceof Integer){
s = String.valueOf(s);
}
ret += s + ", ";
}
return ret;
}catch(Exception e){
System.out.println("In TheWork Execute SQL connection or variable assignment failed.");
e.printStackTrace();
return null;
}
}
}
感谢任何想帮助我的人。
Oct 29, 2013 9:51:03 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Test] in context with path [/test-app] threw exception
java.lang.NullPointerException
at internalLogic.TheWork.Execute(TheWork.java:26)
at testPackage.TestServlet.doGet(TestServlet.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
最佳答案
在您的Execute()
方法中,实例conn
可能是null
。
我认为在您的TheWork
类中:
private DBInterface conn;
应该
private Connection conn;
并在构造函数中
conn = (DBInterface) DBInterface.Conn();
应该简单
conn = DBInterface.Conn();
更新:(以上建议的更正无效)
在您发表最后评论之后,我已经意识到发生了什么事。真正的问题是您需要从
DBInterface
方法返回Conn()
的实例。如果您采用原始代码并像下面所做的那样仅更改方法
Conn()
,它将解决此问题...public static DBInterface Conn() throws SQLException, ClassNotFoundException {
String url = "jdbc:mysql://localhost:3306/cmwebapp";
String user = "user";
String pw = "password";
DBInterface dbInterface = new DBInterface();
try {
Class.forName("com.mysql.jdbc.Driver");
dbInterface.conn = (Connection) DriverManager.getConnection(url, user, pw);
} catch (SQLException e) {
System.out.println("MySQL Connection failed.");
e.printStackTrace();
}
return dbInterface;
}