Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help required.

 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On of the application making a connection to database is throwing null pointer exception. When traced following is my observation.
I am able to get the connection object. I used isClosed() to check the connection which is returning false. Hence the connection is valid.
but when i use createStatement() method it is returning null. What would be the possible reason....
Thanks in advance for the help...

I also tried to use isReadOnly method to check but i was reciving
java.sql.SQLException: java.lang.ClassCastException at weblogic.jdbc.rmi.SerialConnection.isReadOnly(SerialConnection.java:330) ....

[ April 03, 2006: Message edited by: Arun Boraiah ]
[ April 04, 2006: Message edited by: Bear Bibeault ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're going to need to post some code and the stack trace, and point out the line where the NullPointer is being thrown. (try to keep the information short and concise)

Until then we'd just be guessing.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for response David O'Meara.
what i had written in my posting is about what i observed. In short i am able to get the Connection object which is valid. But using the connection object when i calling createStatement() method which is returing null instead of Statement object.

What would be possible cause of getting null instead of statment object?
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What would be possible cause of getting null instead of statment object?


Something wrong in your code, most likely...
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What would be possible cause of getting null instead of statment object?

Hard to say given the information you have provided. Could you post the stack trace? And the code you are trying to run? It should help you get an answer since, as David points out, without it we can only guess.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public synchronized Connection getConnection() throws SQLException {
String JNDINAME = Config.get("CAFE_DATASOURCE_JNDI_NAME");
try {
String url="t3://139.76.173.5:7053";
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL,url);
Context ctx = new InitialContext(ht);
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup(JNDINAME);
Connection connection = ds.getConnection();
return connection;
}
catch(Exception e){
throw new SQLException("Can't make the initial connection. " );
}
}

public Statement getStatement( ) throws DatabaseException {
try
{
CafeConnectionResource myDb = CafeConnectionResource.getInstance();
Connection myConn = myDb.getConnection();
Statement stmt = myConn.createStatement( );
System.out.println("PRINT NORMAL CONNECTION");
System.out.println("myConnectiionCLOSED " +myConn.isClosed());
System.out.println("myStatement " +stmt);
}
catch( Exception e ) {
Log.debug( e.getMessage( ) );
e.printStackTrace( );
throw new DatabaseException( e.getMessage( ) );
}
return( stmt );
}
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything particularly wrong with that code...

In your first post, you imply that stmt is going to be null a this point; if so, you should be getting a NullPointerException on this line. Do you?

A stack trace would be helpful.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am printing statement. Object in system.out.println to shown that statement object is null. I am not getting any stack trace. Since in the code i was using statement object I was geting null pointer exception.
To debug i just checked the connection object with isClosed method, which is returing false, which indicate that connection is right... but just not getting the statment object......
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am not getting any stack trace

You must be if you are getting a NullPointerException .
 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Boraiah:
i am printing statement. Object in system.out.println to shown that statement object is null. I am not getting any stack trace. Since in the code i was using statement object I was geting null pointer exception.
To debug i just checked the connection object with isClosed method, which is returing false, which indicate that connection is right... but just not getting the statment object......


Null is not the same as a nullPointerException.

Statement.toString() can return anything, literally anything at all; it's behavior is not defined by the JDBC standard and therefore printing a Statement object is not guaranteed to be useful or at all diagnostic. (The driver implementer may do something useful/diagnostic, but is not required to do so). Returning "null" is a unusual, but not wrong, and does not mean that the Statement is null.

In other words, it looks like your code is working fine, at least to this point.
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun Boraiah:

public Statement getStatement( ) throws DatabaseException {
try
{
CafeConnectionResource myDb = CafeConnectionResource.getInstance();
Connection myConn = myDb.getConnection();
Statement stmt = myConn.createStatement( );
System.out.println("PRINT NORMAL CONNECTION");
System.out.println("myConnectiionCLOSED " +myConn.isClosed());
System.out.println("myStatement " +stmt);
}
catch( Exception e ) {
Log.debug( e.getMessage( ) );
e.printStackTrace( );
throw new DatabaseException( e.getMessage( ) );
}
return( stmt );
}




Do you have more than one stmt declaration in your program. What I see you have declared statement inside try {} block


Which cleary leads that it does not have any scope outside your try{} block
then how are you returning a stmt from your method.

If you do not have other declaration then how does your code get compile.
If yes then you would return a null statement from your method. and further if you use same You will get a NullPointerException



Shailesh
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Apologies not responding in time, I was able to resolve the problem. The cause for the problem was not with in the code, but it was in environment setting. Classpath was pointing to wrong database driver. On correcting the classpath the application restored to it�s normal behavior.

Thanks for your time....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic