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.

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.

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
So, you get a NullPointerException when executing
This means that
connis null.Where is
conninitialized? Just before, in the call toopenDatabase().How does
openDatabase()initialize the variable?The abobe indicates that either
ds.getConnection()returns a connection, andconncan’t be null (but it is), orconnis null becauseds.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: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:
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: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.