Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong with my code ??

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I'm new to JAVA, I'm trying to set up a connection pool for my application. First of all, I create a object called traveler for storing the DB collection and its activate time by calling the initPool method.

It can be compiled without any error, However, it always throws with "NullPointerException" when connection is passed to the traveler by setConnection method.


Is there something wrong ? ? why ?? anyone help ??


======================================================

public void initPool( String driverName, String url,
String username, String password )
throws SQLException {
try {
Connection c;
traveler tr[] =new traveler[maxConnections];
freePool=new Vector( maxConnections ) ;
activePool=new Hashtable( maxConnections ) ;
Class.forName( driverName ) ;
for( int i=0 ; i<maxConnections ; i++ ) {
c=DriverManager.getConnection(url ,username ,password );
tr.setConnection(c); <= *** problem
freePool.add(tr);
}
}
catch( Exception ex ) {
activePool=null ;
freePool=null ;
throw new SQLException( ex.toString() ) ;
}
}
==========================================================
// traveler declaration
package db;
import java.lang.*;
import java.sql.*;
import java.util.*;

public class traveler {
private static final long expireDuration=50000;
long retrivalTime;
Connection conn;


public void traveler(){
retrivalTime =0;
conn = null;
}

public void traveler(Connection c){
this.conn=c;
}

public void setRetrievalTime() {
retrivalTime = System.currentTimeMillis();
}

public int expiredSection() {
long currentTime = System.currentTimeMillis();
if ( (currentTime - retrivalTime ) > expireDuration ) {

return 1;
}

return 0;
}
public Connection getConnection() {
return this.conn;
}

public void setConnection(Connection c) {
this.conn=c;
}
}
 
Hi,

2 points which I guess that you have missed out:

1) you created an array of traveler -> traveler tr[] =new traveler[maxConnections];
but then when you access it later, you used tr instead of tr[1] etc.. -> tr.setConnection(c);

What you can do is tr.setConnection(c);

2) if you want to do tr.setConnection(c); you must first create/initialise the traveler object first. By doing -> traveler tr[] = new traveler[maxConnections]; doesn't create/initialise the traveler object so during runtime, you will get a nullpointerexception.

So instead of doing -> tr.setConnection(c)
you can either do this -> tr = new Traveler(c);
or this -> tr = new Traveler; tr.setConnection(c);

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top