1

这行代码我得到一个空指针异常

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 org.ari.DatabaseLogic.getData(DatabaseLogic.java:80)
  • [24/Dec/2012:11:59:24] 警告 (27454): CORE3283: stderr: at org.ari.ARIServlet.doPost(ARIServlet.java:72)
  • [24/Dec/2012:11:59:24] 警告 (27454): CORE3283: stderr: at javax.servlet.http.HttpServlet.service(HttpServlet.java:807)
  • [24/Dec/2012:11:59:24] 警告 (27454): CORE3283: stderr: at javax.servlet.http.HttpServlet.service(HttpServlet.java:908)
  • [24/Dec/2012:11:59:24] 警告 (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;
    }

}

我在我的网络服务器上设置了一个连接池并且它正在运行。

还有其他想法或建议吗?

谢谢

圣诞快乐,节日快乐!

4

2 回答 2

3

你打电话openDatabase前打电话getData吗?

如果你不是那么conn就是null并且conn.createStatement会触发空指针异常

于 2012-12-24T11:37:13.350 回答
3

尝试

if (conn == NULL) { openDatabase(); }

就在您的 createStatement 之前

于 2012-12-24T11:38:38.030 回答