1

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!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
tomaytotomato
  • 3,788
  • 16
  • 64
  • 119
  • Is this JNDI jdbc/POOL available, I hope program not able to find JNDI that's why you may be getting NPE – Pradeep Simha Dec 24 '12 at 11:35
  • @PradeepSimha ; I have a jdbc pool and JNDI setup. Is there a way to confirm this – tomaytotomato Dec 24 '12 at 11:38
  • I think you are facing problems said by answers below. Check if that works. – Pradeep Simha Dec 24 '12 at 11:39
  • It surprises me sadly very much that none of the commenters/answerers so far have spotted **severe** threadsafety and resource leaking problems in the code shown so far. This code is a disaster in a multi-user environment such as a web application. To learn more about your mistakes, start here: http://stackoverflow.com/questions/9428573/is-it-safe-to-use-a-static-connection-java-sql-object-in-a-multithreaded-syste – BalusC Dec 25 '12 at 03:28

2 Answers2

3

Are you calling openDatabase before calling getData?

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

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
3

try

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

Just ahead of your createStatement

Vorsprung
  • 32,923
  • 5
  • 39
  • 63
  • Ok I used your code and indeed conn is NULL however my openDatabase does not seem to be setting it for some reason, could it be because its a static variable? – tomaytotomato Dec 24 '12 at 11:44