Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9069785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:31:13+00:00 2026-06-16T17:31:13+00:00

Following on from my previous question I have found the culprit in my code

  • 0

Following on from my previous question I have found the culprit in my code is this object

I have an object called “conn” from the class Connection in Java.

In my old code which works there is no highlighting of the objects name. As you can see below.

enter image description here

However in my newer version of code on Eclipse which does not work due to that object being null all the time the object is highlighted.

enter image description here

My DatabaseLogic class

    package org.ari;

//class dependencies
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DatabaseLogic
{
    private static Connection conn;

    public static String 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

        String result = ds.toString();


        try
        {
            conn = ds.getConnection();
        }
        catch (SQLException e)
        {
            System.out.println( e.toString());          
        }


        return result;
    }

    public static void closeDatabase()
    {
        try
        {
            conn.close();
        }
        catch (SQLException e)
        {

        }
    }

    // queryId is the parameter to be used for querying for relevant records
    // - possibly change name to make it less specific e.g. recordid
    public static String getData(String queryId, int requestNumber)
            throws SQLException
    {
        String result = "";
        if (queryId != null)
        {
            try
            {

                if (conn == null)
                { 
                    //result = "We are in here";
                    result = openDatabase(); 
                }

                // prepare a statement for use in query

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

}

What does this highlighting mean, I am guessing eclipse is treating this object in a different way and this is why this object is no longer functioning as it should.

How do I fix this, any ideas? I have tried restoring my older version of code but this highlighting is still the same.

I am guessing it is some setting somewhere in the IDE.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:31:15+00:00Added an answer on June 16, 2026 at 5:31 pm

    So, you get a NullPointerException when executing

    conn.createStatement();
    

    This means that conn is null.

    Where is conn initialized? Just before, in the call to openDatabase().

    How does openDatabase() initialize the variable?

        try
        {
            conn = ds.getConnection();
        }
        catch (SQLException e)
        {
            System.out.println( e.toString());          
        }
    

    The abobe indicates that either ds.getConnection() returns a connection, and conn can’t be null (but it is), or conn is null because ds.getConnection() throws a SQLException, but you continue as if nothing happened, just displaying the toString() of the exception that was thrown. The above should be rewritten as the following:

    conn = ds.getConnection();
    

    That way, you wouldn’t ignore the SQLException. It would propagate to the caller, and would be identified as a SQLException, with a clear error message, indicating where the problem comes from in the code, rather than an obscure NullPointerException happening later, and being much harder to diagnose.

    Let the exception propagate, or at the very least, replace the code by:

    try {
        conn = ds.getConnection();
    }
    catch (SQLException e) {
        throw new RuntimeException("Something really bad happened, and it makes no sense to continue further, so I'll throw a runtime exception wrapping the original cause", e);
    }
    

    Now you just need to get the stack trace of the SQLException thrown by ds.getConnection(), and understand what it’s error message means.


    Note that I suspect that the exception is not thrown by conn.createStatement(), but by some other exception that you also ignore in the following:

    catch (Exception e)
    {
        // log.error("Cannot connect to database :" + e);
    }
    

    Once again, don’t catch the exception. Let it propagate. By catching it, you make it very hard for yourself to notice the problem, and impossible to diagnose it.

    If you can’t handle an exception, don’t catch it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code (from a previous question on this site) which retrieves
I have the following code adapted from a previous question/answer: var str = $('title').text();
Following on from my previous question I have been working on getting my object
Following on from a previous question, I have the following code: var results =
Okay, this is following on from my previous question reguarding performing a simple ajax
This problem follows on from a previous question . When I run the following
This isn't a repeat of a previous question, I have found out the issue
Following on from my previous question , I have now managed to rotate my
Following up from my previous question. Can anyone explain why the following code compiles
From a previous question , I have the following: So I have implemented a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.