我得到一個NullPointerException與這行代碼NullPointerException異常與連接對象創建的SQL語句
Statement stmt = conn.createStatement();
使用這是工作確定了幾天回來,我從以前的版本我班上的回收該servlet我的存儲庫,它仍然無法正常工作,所以我猜測這可能是一個服務器端問題。
但是,爲了確保我想獲得儘可能多的信息,最好的調試方法是什麼。
我一直在使用
e.toString()
然而,這只是給 「空指針異常」 消息試過。
堆棧跟蹤
- [24/DEC/2012:11:59:24]警告(27454):CORE3283:標準錯誤:java.lang中。 NullPointerException
- [24/Dec/2012:11:59:24] warning(27454):CORE3283:stderr:at org.ari.DatabaseLogic.getData(DatabaseLogic.java:80)
- [24/Dec/2012:11:59:24] warning(27454):CORE3283:stderr:at org.ari.ARIServlet.doPost(ARIServlet.java:72)
- [24/Dec/2012:11警告(27454):CORE3283:stderr:在javax.servlet.http.HttpServlet.service(HttpServlet.java:807)
- [24/Dec/2012:11:59:24] warning(27454) ):CORE3283:stderr:at javax.servlet.http.HttpServlet.service(HttpServlet.java:908)
- [24/Dec/2012:11:59:24] warning(27454):CORE3283:stderr:at org .apache.catalina.core.StandardWrapperValve.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服務器上的連接池建立並開始運行。
任何其他想法或建議?
謝謝
聖誕快樂和節日快樂!
的這是JNDI的JDBC/POOL可用,我希望節目沒能找到JNDI這就是爲什麼你可能會得到NPE –
@PradeepSimha;我有一個jdbc池和JNDI設置。有沒有辦法確認這個 – kaleeway
我想你正面臨着下面答案所說的問題。檢查是否有效。 –