Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Null pointer exception (http://www.velocityreviews.com/forums/t145087-null-pointer-exception.html)

dschectman@yahoo.com 07-25-2005 03:10 PM

Null pointer exception
 
I get a stange NullPointerException in getClass().getName(). The code
below is running on Iplanet app server 6.5 running on windows 2000.
The datasource is defined by an app server connection pool.

Here is the code
package helper;

import org.apache.log4j.Category;
import java.sql.*;

// This class gets an connection to the datasource
public class DBConnectionHelper
{
Category loggerObj = Category.getInstance(("DBConnectionHelper");
Connection conn = null;
com.netscape.server.jdbc.ConnectionProxy csproxy = null;

public DBConnectionHelper()
{
}

public Connection getDBConnection() {
return getDBConnection("defaultdb");
}
public Connection getDBConnection(String connName)
{
Connection csproxyconn = null;
try
{
loggerObj.info("getting connection");
// Get a connection to the datasorce
conn = new helper.DBConnectionManager().getDBConnection(
connName);
loggerObj.info("Got connection");
loggerObj.info(conn); // The funny thing is that this line logs a
valid class (com.netscape.server.jdbc.ConnectionProxy@1a4bd4)
loggerObj.info(conn.getClass()); // Yet this line throws a
NullPOinterException as if conn was null
loggerObj.info(conn.getClass().getName());
String className = conn.getClass().getName();
loggerObj.info("Connection class Name ->" + className);

//For Iplanet connection pool, convert to Oracle connection
if
(!className.equalsIgnoreCase("oracle.jdbc.driver.O racleConnection")) {
csproxy = (com.netscape.server.jdbc.ConnectionProxy) conn;
csproxyconn = csproxy.getConnection();
return csproxyconn;
}
}
catch (Exception ex) {
loggerObj.error("Exception caught");
loggerObj.error(ex.getMessage());
loggerObj.error(ex);
//loggerObj.error(ex.getMessage(), ex);
}

return conn;
}
}

The connection manager class is just a wrapper for getting the
connection. A configuration file identifies whether to get it from a
JNDI lookup or from Oracle.

public Connection getDBConnection(String connName)
{
loggerObj.info("in getDBConnection" + connName);
// Get the db configuration from the config file
DBConfig aDBConfig = DBConfig.getInstance();
OrderedHashMap lookupData = aDBConfig .getLookupData(DBConfig
..KEY_DBCONNECTION);
InitialContext initialContext = null;
Hashtable env = new java.util.Hashtable(1);
Connection conn = null;
String oracleUserName = null;
String oracleUserPassword = null;
String oracleTnsName = null;
String oracleDriverName = null;
String jndiLookupType = null;
String jndiName = null;
String jndiLookup = null;

if (lookupData != null)
{
OrderedHashMap dbLookupData = (OrderedHashMap) if
(dbLookupData != null) {
oracleUserName = (String) dbLookupData.get("username");
oracleUserPassword = (String) dbLookupData.get("password");
oracleTnsName = (String) dbLookupData.get("dburl");
oracleDriverName = (String) dbLookupData.get("driver");
jndiLookupType = (String)
dbLookupData.get("jndi_lookup_type");
jndiName = (String) dbLookupData.get("jndi_name");
jndiLookup = (String) dbLookupData.get("jndi_lookup");

try {
// To use Iplanet connection pooling
if ( (jndiLookup != null) &&
jndiLookup.equalsIgnoreCase("yes")) {
loggerObj.info("INSIDE Iplanet Connection Pool
getConnection(" +
jndiName + ")");
initialContext = new InitialContext(env);
DataSource ds = (javax.sql.DataSource)
initialContext.lookup(
jndiName);
conn = ds.getConnection();

// com.netscape.server.jdbc.ConnectionProxy csproxy =
(com.netscape.server.jdbc.ConnectionProxy)ds.getCo nnection();
//
System.out.println("csproxy.getConnection().getCla ss().getName(): " +
csproxy.getConnection().getClass().getName());
// conn = csproxy.getConnection();
// loggerObj.info("conn.getClass().getName()"
+conn.getClass().getName());

loggerObj.info("Completed Iplanet Connection Pool
getConnection(" +
jndiName + ")");
}
else { // To use Oracle connection pooling
loggerObj.info("INSIDE Oracle Connection getConnection("
+ connName +
")");
Driver d = (Driver)
Class.forName(oracleDriverName).newInstance();
conn = DriverManager.getConnection(oracleTnsName,
oracleUserName,
oracleUserPassword);
loggerObj.info("Completed Oracle Connection
getConnection(" +
connName + ")");
}
}
catch (SQLException e) {
loggerObj.error("Error in getting the NEW Connection " +
e.getMessage());
}
catch (NamingException e) {
loggerObj.error("Error in getting the NEW Connection " +
e.getMessage());
}
catch (Exception ex) {
loggerObj.error(ex.getMessage(), ex);
}
finally {
return conn;
}
} else loggerObj.error("dbLookupData is null");
} else loggerObj.error("lookupData is null");
return conn;
}

Here are the log files
[25/Jul/2005 10:01:37:2] info: [25 Jul 2005 10:01:37] getting
connection
[25/Jul/2005 10:01:37:2] info: [25 Jul 2005 10:01:37] INSIDE Iplanet
Connection Pool getConnection(jdbc/defaultdb)
[25/Jul/2005 10:01:37:6] info: [25 Jul 2005 10:01:37] Completed Iplanet
Connecti
on Pool getConnection(jdbc/defaultdb)
[25/Jul/2005 10:01:37:6] info: [25 Jul 2005 10:01:37] Got connection
[25/Jul/2005 10:01:37:6] info: [25 Jul 2005 10:01:37]
com.netscape.server.jdbc.ConnectionProxy@1a4bd4
[25/Jul/2005 10:01:37:6] error: [25 Jul 2005 10:01:37] Exception caught
[25/Jul/2005 10:01:37:6] error: [25 Jul 2005 10:01:37]
[25/Jul/2005 10:01:37:6] error: [25 Jul 2005 10:01:37]
java.lang.NullPointerException


John Currier 07-25-2005 03:31 PM

Re: Null pointer exception
 
You're evidently throwing away your stack traces. I assume that your
logger can log an exception with stack traces, right? That extra piece
of info should significantly help isolate the problem.

IMHO normal code should not call Exception.getMessage().

John Currier
http://schemaspy.sourceforge.net


Thomas Fritsch 07-25-2005 03:39 PM

Re: Null pointer exception
 
dschectman@yahoo.com wrote:
> I get a stange NullPointerException in getClass().getName(). ...

How do you know that the NullPointerException happened in
getClass().getName()? I see no evidence for this in your material below.
Do you have an exception stack trace leading to that conclusion? Please
share that information with us. If you don't have a stack trace, let
your loggerObj print one. If the stack trace gives any line numbers,
please mark-up those lines in the posted source code.

> ... The code
> below is running on Iplanet app server 6.5 running on windows 2000.
> The datasource is defined by an app server connection pool.
>
> Here is the code

[...code snipped...]
BTW: the line-wrapping makes the code very hard to read.
>
> The connection manager class is just a wrapper for getting the
> connection. A configuration file identifies whether to get it from a
> JNDI lookup or from Oracle.
>

[...code snipped...]
>
> Here are the log files
> [25/Jul/2005 10:01:37:2] info: [25 Jul 2005 10:01:37] getting
> connection
> [25/Jul/2005 10:01:37:2] info: [25 Jul 2005 10:01:37] INSIDE Iplanet
> Connection Pool getConnection(jdbc/defaultdb)
> [25/Jul/2005 10:01:37:6] info: [25 Jul 2005 10:01:37] Completed Iplanet
> Connecti
> on Pool getConnection(jdbc/defaultdb)
> [25/Jul/2005 10:01:37:6] info: [25 Jul 2005 10:01:37] Got connection
> [25/Jul/2005 10:01:37:6] info: [25 Jul 2005 10:01:37]
> com.netscape.server.jdbc.ConnectionProxy@1a4bd4
> [25/Jul/2005 10:01:37:6] error: [25 Jul 2005 10:01:37] Exception caught
> [25/Jul/2005 10:01:37:6] error: [25 Jul 2005 10:01:37]
> [25/Jul/2005 10:01:37:6] error: [25 Jul 2005 10:01:37]
> java.lang.NullPointerException
>



--
"Thomas:Fritsch$ops:de".replace(':','.').replace(' $','@')


dschectman@yahoo.com 07-25-2005 07:25 PM

Re: Null pointer exception
 
Thanks for the replies. I added some more debugging to the code

loggerObj.info("Getting conn " + connName);
conn = new helper.DBConnectionManager().getDBConnection(connN ame);
loggerObj.info("Got it");
loggerObj.info(conn);
if (conn==null) throw new Exception("conn is null");
loggerObj.info(String.valueOf(conn.isClosed()));
loggerObj.info(String.valueOf(conn.getAutoCommit() ));
loggerObj.info(conn.getClass());
String className = conn.getClass().getName();
loggerObj.info("Connection class Name ->" + className);

According to the log file, the exception occurs on conn.getClass() as
the booleans are recorded in the log file and the next entry is the
NullPointerException.

[25/Jul/2005 15:02:59:0] info: [25 Jul 2005 15:02:59] Got it
[25/Jul/2005 15:02:59:0] info: [25 Jul 2005 15:02:59]
com.netscape.server.jdbc.ConnectionProxy@11e831
[25/Jul/2005 15:02:59:0] info: [25 Jul 2005 15:02:59] false
[25/Jul/2005 15:02:59:0] info: [25 Jul 2005 15:02:59] true
[25/Jul/2005 15:02:59:0] error: [25 Jul 2005 15:02:59]
java.lang.NullPointerException

According to the stack trace the error occurred at line 29, which,
according to the editor, is
conn = new helper.DBConnectionManager().getDBConnection(connN ame);

[25/Jul/2005 15:02:59:0] error: [25 Jul 2005 15:02:59]
java.lang.NullPointerException
at helper.DBConnectionHelper.getDBConnection(
DBConnectionHelper.java:29)
at jsp.APPS.VxComponent.test._jspService(test.java:15 0)
at org.apache.jasper.runtime.HttpJspBase.service(Unkn own
Source)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
at
com.netscape.server.servlet.servletrunner.ServletI nfo.service
(Unknown Source)
at
com.netscape.server.servlet.servletrunner.ServletR unner.callJSP(Unknown
Source)
at
com.netscape.server.servlet.platformhttp.PlatformH ttpServletResponse.
callJspCompiler(Unknown Source)
at
com.netscape.server.servlet.platformhttp.PlatformH ttpServletResponse.
callUri(Unknown Source)
at
com.netscape.server.servlet.platformhttp.PlatformH ttpServletResponse.
callUriRestrictOutput(Unknown Source)
at
com.netscape.server.servlet.platformhttp.PlatformR equestDispatcher.fo
rward(Unknown Source)
at com.netscape.server.servlet.jsp.JSPRunner.service( Unknown
Source)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
at
com.netscape.server.servlet.servletrunner.ServletI nfo.service(Unknown
Source)
at
com.netscape.server.servlet.servletrunner.ServletR unner.execute(Unknown
Source)
at com.kivasoft.applogic.AppLogic.execute(Unknown Source)
at com.kivasoft.applogic.AppLogic.execute(Unknown Source)
at com.kivasoft.thread.ThreadBasic.run(Native Method)
at java.lang.Thread.run(Thread.java:479)

Thomas Fritsch wrote:
> dschectman@yahoo.com wrote:
> > I get a stange NullPointerException in getClass().getName(). ...

> How do you know that the NullPointerException happened in
> getClass().getName()? I see no evidence for this in your material below.
> Do you have an exception stack trace leading to that conclusion? Please
> share that information with us. If you don't have a stack trace, let
> your loggerObj print one. If the stack trace gives any line numbers,
> please mark-up those lines in the posted source code.
>
> > ... The code
> > below is running on Iplanet app server 6.5 running on windows 2000.
> > The datasource is defined by an app server connection pool.
> >
> > Here is the code

> [...code snipped...]
> BTW: the line-wrapping makes the code very hard to read.
> >
> > The connection manager class is just a wrapper for getting the
> > connection. A configuration file identifies whether to get it from a
> > JNDI lookup or from Oracle.
> >

> [...code snipped...]
> >
> > Here are the log files
> > [25/Jul/2005 10:01:37:2] info: [25 Jul 2005 10:01:37] getting
> > connection
> > [25/Jul/2005 10:01:37:2] info: [25 Jul 2005 10:01:37] INSIDE Iplanet
> > Connection Pool getConnection(jdbc/defaultdb)
> > [25/Jul/2005 10:01:37:6] info: [25 Jul 2005 10:01:37] Completed Iplanet
> > Connecti
> > on Pool getConnection(jdbc/defaultdb)
> > [25/Jul/2005 10:01:37:6] info: [25 Jul 2005 10:01:37] Got connection
> > [25/Jul/2005 10:01:37:6] info: [25 Jul 2005 10:01:37]
> > com.netscape.server.jdbc.ConnectionProxy@1a4bd4
> > [25/Jul/2005 10:01:37:6] error: [25 Jul 2005 10:01:37] Exception caught
> > [25/Jul/2005 10:01:37:6] error: [25 Jul 2005 10:01:37]
> > [25/Jul/2005 10:01:37:6] error: [25 Jul 2005 10:01:37]
> > java.lang.NullPointerException
> >

>
>
> --
> "Thomas:Fritsch$ops:de".replace(':','.').replace(' $','@')



Thomas Fritsch 07-25-2005 10:08 PM

Re: Null pointer exception
 
<dschectman@yahoo.com> wrote:
> Thanks for the replies. I added some more debugging to the code
>
> loggerObj.info("Getting conn " + connName);
> conn = new helper.DBConnectionManager().getDBConnection(connN ame);
> loggerObj.info("Got it");
> loggerObj.info(conn);
> if (conn==null) throw new Exception("conn is null");
> loggerObj.info(String.valueOf(conn.isClosed()));
> loggerObj.info(String.valueOf(conn.getAutoCommit() ));
> loggerObj.info(conn.getClass());
> String className = conn.getClass().getName();
> loggerObj.info("Connection class Name ->" + className);
>
> According to the log file, the exception occurs on conn.getClass() as
> the booleans are recorded in the log file and the next entry is the
> NullPointerException.
>
> [25/Jul/2005 15:02:59:0] info: [25 Jul 2005 15:02:59] Got it
> [25/Jul/2005 15:02:59:0] info: [25 Jul 2005 15:02:59]
> com.netscape.server.jdbc.ConnectionProxy@11e831
> [25/Jul/2005 15:02:59:0] info: [25 Jul 2005 15:02:59] false
> [25/Jul/2005 15:02:59:0] info: [25 Jul 2005 15:02:59] true
> [25/Jul/2005 15:02:59:0] error: [25 Jul 2005 15:02:59]
> java.lang.NullPointerException
>
> According to the stack trace the error occurred at line 29, which,
> according to the editor, is
> conn = new helper.DBConnectionManager().getDBConnection(connN ame);
>
> [25/Jul/2005 15:02:59:0] error: [25 Jul 2005 15:02:59]
> java.lang.NullPointerException
> at
> helper.DBConnectionHelper.getDBConnection(DBConnec tionHelper.java:29)
> at jsp.APPS.VxComponent.test._jspService(test.java:15 0)

[...]
> at java.lang.Thread.run(Thread.java:479)
>

You actually have 2 (two!) NullPointerException's:
(1) one at line 29 in method getDBConnection() of the DBConnectionManager
(*not* DBConnectionHelper) class, as the exception stack trace shows
without doubt
(2) one near the place where you suspect it.But you decided to catch it
without printing the stack trace, thus ignoring the line numbers you
would get there

The reason behind that is probably your somewhat screwed (sorry!) design.
You actually have 2 different classes (DBConnectionHelper and
DBConnectionManager), both with a getDBConnection method.
In your DBConnectionHelper.getDBConnection method you create a new instance
of DBConnectionManager and call its getDBConnection method. O my
goodness....
You probably should redesign your code from scratch.
To make it clear, here is your current code with large parts stripped out:

public class DBConnectionHelper
{
[...]

public Connection getDBConnection(String connName)
{
[...]
conn = new helper.DBConnectionManager().getDBConnection( /*line 29*/
connName);
[...]
return conn;
}
}

Secondly: As John Currier has already recommended, you *really* should print
a stack trace in your catch clause. Look up the javadoc of class
org.apache.log4j.Category to find a method, which can take an Exception (or
Throwable) object as a parameter.

--
"TFritsch$t-online:de".replace(':','.').replace('$','@')



Thomas Fritsch 07-26-2005 12:13 AM

Re: Null pointer exception
 
"Thomas Fritsch" <i.dont.like.spam@invalid.com> wrote:
> <dschectman@yahoo.com> wrote:

[...]
>> java.lang.NullPointerException
>> at
>> helper.DBConnectionHelper.getDBConnection(DBConnec tionHelper.java:29)
>> at jsp.APPS.VxComponent.test._jspService(test.java:15 0)

> [...]
>> at java.lang.Thread.run(Thread.java:479)
>>

> You actually have 2 (two!) NullPointerException's:
> (1) one at line 29 in method getDBConnection() of the DBConnectionManager
> (*not* DBConnectionHelper) class, as the exception stack trace shows
> without doubt

I was puzzled. Of course it is just the other way round:
(1) one at line 29 in method getDBConnection() of the DBConnectionHelper
(*not* DBConnectionManager) class, as the exception stack trace shows
without doubt
[...]
But the main issue still persists.

--
"TFritsch$t-online:de".replace(':','.').replace('$','@')



John Currier 07-26-2005 01:34 PM

Re: Null pointer exception
 
> According to the stack trace the error occurred at line 29, which,
> according to the editor, is
> conn = new helper.DBConnectionManager().getDBConnection(connN ame);
>
> [25/Jul/2005 15:02:59:0] error: [25 Jul 2005 15:02:59]
> java.lang.NullPointerException
> at helper.DBConnectionHelper.getDBConnection(DBConnec tionHelper.java:29)
> at jsp.APPS.VxComponent.test._jspService(test.java:15 0)
> at org.apache.jasper.runtime.HttpJspBase.service(Unkn own Source)


That's not line 29 of your JSP. It's line 29 of
DBConnectionHelper.java.

I won't even comment on doing database manipulations directly in a
JSP...

John
http://schemaspy.sourceforge.net


dschectman@yahoo.com 07-27-2005 01:35 PM

Re: Null pointer exception
 
Thanks for the input. This code is from a production application that
we inherited. The same ambiguous code runs fine in production. I have
traced the porblem to my setup. I created a function that gets the
datasource with a JNDI lookup and prints the class associated with it.
1) If the object is cast to com.netscape.server.jdbc.ConnectionProxy,
getClass does not throw a null pointer exception
2) If the object is cast as Connection, getClass throws a null pointer
exception

The code compiles without any errors. This tells me that something is
wrong with the runtime classpath or the implementation of
com.netscape.server.jdbc.ConnectionProxy. The null pointer exception
may be related to a class cast exception between Connection and
com.netscape.server.jdbc.ConnectionProxy.

test.jsp:
<html>
<body>
Test datasource
<%
TestClass c = new TestClass();
c.foo();
%>
</body>
</html>

TestClass.java
import org.apache.log4j.Logger;
import java.sql.*;
import javax.naming.InitialContext;
import java.util.Hashtable;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.StringWriter;
import java.io.PrintWriter;

public class TestClass {
Logger loggerObj = getLogger("DBConnectionManager");
public void foo(String jndiName)
throws Exception
{
Hashtable env = new Hashtable();
InitialContext initialContext = new InitialContext(env);
loggerObj.info("in foo");
DataSource ds = (javax.sql.DataSource)
initialContext.lookup(jndiName);
loggerObj.info("got datasource");
Connection c =ds.getConnection();
com.netscape.server.jdbc.ConnectionProxy csproxy =
(com.netscape.server.jdbc.ConnectionProxy) ds.getConnection();
loggerObj.info("cl1 = " + csproxy.getClass().getName());
loggerObj.info("cl2 = " +
((com.netscape.server.jdbc.ConnectionProxy)c).getC lass().getName());
loggerObj.info("cl2 = " + ((Connection)
csproxy).getClass().getName());
}
}


John Currier 07-29-2005 02:01 AM

Re: Null pointer exception
 
I'm confused as to why you posted the code, but didn't post the stack
trace.

What you cast the connection to should not result in any change in the
behavior of getClass(). That cast is not changing anything related to
getClass().

Casting is usually a questionable thing to do. That is, you'd better
have a darn good reason for doing it.

With what you've given us to go on, I would bet that ds.getConnection()
is returning null.

John
http://schemaspy.sourceforge.net


dennishancy@eaton.com 08-17-2005 01:46 PM

Re: Null pointer exception
 
I am having similar error messages that look like this:

java.lang.NullPointerException at euwPledge.doGet(Unknown Source) at
javax.servlet.http.HttpServlet.service(HttpServlet .java:787)

Being new to Java, I have a few questions:

- Does the 787 refer to a line number?
- What's the best way to begin debugging this?
- What other information can I post here that would help get to the
root of the problem?

The really odd part is that I had one occasion when the code worked as
it should. But that was only once. Not sure if that suggests a code
issue or a configuration issue?

Any suggestions as to what I can look into would be greatly
appreciated. Thanks.


Dennis Hancy
Eaton Corporation
Cleveland, OH



All times are GMT. The time now is 04:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.