java NullpointerException与创建SQL语句的连接对象
我在这行代码中得到一个nullpointerexception
Statement stmt = conn.createStatement();
几天前,使用它的servlet工作正常,我从我的存储库中恢复了以前版本的类,但它仍然不工作,所以我猜这可能是服务器端的问题
然而,为了确保我希望获得尽可能多的信息,什么是调试的最佳方式
我试过使用
e.toString()
但是,这只会给出“nullpointerexception”消息
堆栈跟踪
- [24/Dec/2012:11:59:24]警告(27454):CORE3283:stderr:java。lang.NullPointerException
- [24/Dec/2012:11:59:24]警告(27454):CORE3283:stderr:at组织。阿里。数据库逻辑。getData(DatabaseLogic.java:80)
- [24/Dec/2012:11:59:24]警告(27454):CORE3283:stderr:at组织。阿里。阿里瑟夫莱特。doPost(ARIServlet.java:72)
- [24/Dec/2012:11:59:24]警告(27454):CORE3283:stderr:at javax。servlet。http。HttpServlet。服务(HttpServlet.java:807)
- [24/Dec/2012:11:59:24]警告(27454):CORE3283:stderr:at javax。servlet。http。HttpServlet。服务(HttpServlet.java:908)
- [24/Dec/2012:11:59:24]警告(27454):CORE3283:stderr:at组织。阿帕奇。卡塔琳娜。果心标准包装阀。invokeServletService(StandardWrapperValve.java:771)
数据库逻辑类
public class DatabaseLogic
{
private static Connection conn;
public static void openDatabase() throws IOException, SQLException,
NamingException
{
// context class gives naming standards of the surrounding environment
// of the servlet i.e. the web server ,
// allowing the servlet to interface with the web servers resources
Context initialContext = new InitialContext();
Context envContext = (Context) initialContext.lookup("java:comp/env");
// servlet looks up for a connection pool called "jdbc/POOL"
DataSource ds = (DataSource) envContext.lookup("jdbc/POOL");
// connection is then made/requests to connection pool
try
{
conn = ds.getConnection();
}
catch (SQLException e)
{
System.out.println(e.toString());
}
}
// queryId is the parameter to be used for querying for relevant records
public static String getData(String queryId, int requestNumber)
throws SQLException
{
String result = "";
if (queryId != null)
{
try
{
// prepare a statement for use in query
result = "This code breaks at this point";
Statement stmt = conn.createStatement();
// query parameratised with queryId
String qry = "SELECT RECORD_ID, USER_ID, OPERATION_CD, BUSCOMP_NAME, OPERATION_DT, FIELD_NAME, OLD_VAL, NEW_VAL, AUDIT_LOG, ROW_ID, BC_BASE_TBL FROM S_AUDIT_ITEM WHERE RECORD_ID='"
+ queryId + "'";
ResultSet results = stmt.executeQuery(qry);
result = XMLBuilder.xmlBuilder(results, queryId,
requestNumber);
// close the connection
stmt.close();
results.close();
}
catch (Exception e)
{
// log.error("Cannot connect to database :" + e);
}
}
else
{
// not sure if ever reached
result = "The query parameter is a null value";
}
return result;
}
}
我在web服务器上安装了连接池,它正在运行
还有其他想法或建议吗
谢谢
圣诞快乐,节日快乐
# 1 楼答案
试一试
如果(conn==NULL){ openDatabase(); }
就在你发言之前
# 2 楼答案
在调用
getData
之前,您是否正在调用openDatabase
如果没有,则
conn
是null
,conn.createStatement
将触发空指针异常