NullpointerException with connection object creati

2019-07-31 09:34发布

问题:

I am getting a nullpointerexception with this line of code

Statement stmt = conn.createStatement();

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.

I have tried using

 e.toString() 

However this only gives "nullpointerexception" message.

Stack Trace

  • [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)

Database Logic Class

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

}

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

Any other ideas or recommendations?

Thanks

Merry Christmas and Happy Holidays!

回答1:

Are you calling openDatabase before calling getData?

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



回答2:

try

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

Just ahead of your createStatement