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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HashTable for storing various classes 1

Status
Not open for further replies.

developerinlondon

Programmer
Jan 11, 2003
196
GB
I am trying to use a hashtable for storing various classes that needs to be passed as parameter from one class to another.

eg
Code:
class1 {
 ...
 ...
 Connection connection = ...
 HashTable params = new Hashtable();
 params.put("data1",connection);
 Class2 class2 = new Class2(params);
}

class2 {
  private Connection connection;
  ...
  this.connection = params.get("data1");
  // or i tried the following that doesnt work either:
  this.connection = (Connection)params.get("data1");
}

any ideas how I can do this?

thanks
 
Well, for starters, in what way does it not work? Does it not compile, or are you getting exceptions?

Incidentally, why are you storing Connections like this (assuming they are java.sql.Connection objects)? Wouldn't you be better off using a Connection Pool?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Well when I read it off from the second class it gets nothing and is unable to use the connection.
I am using a connection pool and the connection object is taken from the pool, the example above is a simplified version. The connection is taken from the pool in the main servlet and is passed to other classses that the servlet instantiates and uses.
 
ok what i found is if I try to do anything with the connection object that gets passed inside the 2nd class through the HashTable I get thrown out of the class straightaway. Eg connection.toString() throws me out.
 
when I read it off from the second class it gets nothing...

... so you're saying the connection variable ends up holding null?

Do you have to use a String as your Hashtable key? It may be something to do with String 'intern'ing and one literal use of "data1" not matching another.

Try using an Integer value as a key. These could be held as static enumerations in a separate class.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
I do need to use it as a string as the key as otherwise the names would be too cryptic to understand.

I found out what I said earlier on your quote might be wrong as I can actually see all the objects and values in the right places if I do a HashTable.toString() inside the 2nd class. But problem seems to be when I try to extract it out of the HashTable something goes wrong...
I am using:
String url = (String)params.get("url");
Connection con = (Connection)params.get("connection");
etc.

The exception that gets thrown when I try to read these variables (url, con etc) is a 'null' exception.
 
I do need to use it as a string as the key as otherwise the names would be too cryptic to understand.

What's wrong with :-
Code:
public class ParamLookup {
  public static final Integer URL = new Integer(0);
  public static final Integer DATA1 = new Integer(1);
  //...etc
}

//storing in table
HashTable params = new Hashtable();
params.put(ParamLookup.DATA1, connection);

//retrieving
Connection conn = (Connection)params.get(ParamLookup.DATA1);

What's cryptic about that, or am I missing something?



Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Sorry I found the problem! Was a mistake from my part! I left 'this.params' in the 2nd class... very silly mistake. Thanks for the time in looking into this still!
 
[smile]Damn!

Have you tried the same scenario in a stand-alone program instead of a Servlet?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top