NullpointerException与连接对象创建SQL语句 [英] NullpointerException with connection object creating SQL statement

查看:92
本文介绍了NullpointerException与连接对象创建SQL语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这行代码中得到一个nullpointerexception

I am getting a nullpointerexception with this line of code

Statement stmt = conn.createStatement();

使用此方法的servlet几天前就可以正常工作了,我从存储库中恢复了以前版本的类,但仍无法正常工作,因此我猜测这可能是服务器端的问题.

The servlet that uses this was working ok a few days back and I recovered from a previous version of my class from my repository and it still is not working , so I am guessing it is maybe a server side problem.

但是请确保我想获得尽可能多的信息,这是调试的最佳方法.

However to be sure I want to get as much information as possible , what would be the best way to debug.

我尝试使用

 e.toString() 

但是,这仅给出"nullpointerexception"消息.

However this only gives "nullpointerexception" message.

堆栈跟踪

  • [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:位于org.apache.catalina.core.StandardWrapperValve.invokeServletService(StandardWrapperValve.java:771)
    • [24/Dec/2012:11:59:24] warning (27454): CORE3283: stderr: 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:59:24] warning (27454): CORE3283: stderr: at 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服务器上设置了连接池,并且该池正在运行.

      I have a connection pool setup on my web server and it is running.

      还有其他想法或建议吗?

      Any other ideas or recommendations?

      谢谢

      圣诞快乐,节日快乐!

      推荐答案

      在调用getData之前是否先调用openDatabase?

      Are you calling openDatabase before calling getData?

      如果不是,那么connnull,并且conn.createStatement将触发空指针异常

      If you aren't then conn is null and conn.createStatement will trigger the null pointer exception

      这篇关于NullpointerException与连接对象创建SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆