NullpointerException,连接对象创建SQL语句

时间:2012-12-24 11:33:45

标签: java servlets jdbc nullpointerexception

我正在使用这行代码获得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 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)<< li>   
  • [24 / Dec / 2012:11:59:24]警告(27454):CORE3283:stderr:at javax.servlet.http.HttpServlet.service(HttpServlet.java:908)<< li>   
  • [24 / Dec / 2012:11:59:24]警告(27454):CORE3283:stderr:at org.apache.catalina.core.StandardWrapperValve.invokeServletService(StandardWrapperValve.java:771)<< li>   

数据库逻辑类

    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;
    }

}

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

还有其他想法或建议吗?

由于

圣诞快乐,节日快乐!

2 个答案:

答案 0 :(得分:3)

您是否在致电openDatabase之前致电getData

如果不是,则connnullconn.createStatement将触发空指针异常

答案 1 :(得分:3)

尝试

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

就在你的createStatement

之前