hashtable

T

Tuurbo46

Hi

Im currently having a bit of a problem assesing a hashtable. The below code
compiles ok, but nothing ends up in my hashtable. Could somebody offer me
some advice on where im going wrong. Also i would like to retrieve this
data and display it? Would i use the .get method for this?

}
Block b[] = cc(motorModel);

MotorBike plane = new MotorBike(b, motorRegisNum, motorModel);
if (!motorBike.containsKey(motorRegisNum))
{
try {
motorBike.put(MotorRegisNum, motor);
}
catch(Exception ex){
System.out.println("Problem Adding motorbike to a hash
table");
}


}

Thanks tuurbo
 
C

cbroussard

Did you retype this, or did a copy & paste? We need to have better
understanding of the types for your variables.

motorRegisNum, is that a string? what's motor? ... yadda yadda yadda.

If you look at java.util.Map interface you'll see

map.put(String, Object) & map.get(String) those are your accessors. on
the get, don't forget to cast it to your class, because it returns
Object.

Besides that all your code looks okay to me, but make sure you're not
recreating your motorBoke object elsewhere.. that will cause it to be
empty. this might be the problem you're running into.. only a guess
though.

www.binaryfrost.com
 
O

Oliver Wong

Tuurbo46 said:
Hi

Im currently having a bit of a problem assesing a hashtable. The below
code compiles ok, but nothing ends up in my hashtable. Could somebody
offer me some advice on where im going wrong. Also i would like to
retrieve this data and display it? Would i use the .get method for this?

}
Block b[] = cc(motorModel);

MotorBike plane = new MotorBike(b, motorRegisNum, motorModel);
if (!motorBike.containsKey(motorRegisNum))
{
try {
motorBike.put(MotorRegisNum, motor);
}
catch(Exception ex){
System.out.println("Problem Adding motorbike to a hash
table");
}


}

Hard to give you a definite answer, because nowhere in this snippet do
you ever declare something to be of type java.util.Hashtable, so I can only
make wild guesses. I'm guessing that "motorBike" is your hashtable. "put" is
the correct method to use, and the only exception it throws is
NullPointerException if either the key or the value is null. Since you never
show MotorRegisNum or motor having their value set anywhere, I can't rule
out that they might be null. You don't show motorBike getting set anywhere
either, so it might be null as well.

To get a value out of a hashtable, yes, you use the "get" methods.

- Oliver
 
P

pit.grinja

Hi Tuurbo,
another wild suggestion:
Im currently having a bit of a problem assesing a hashtable. The below code
compiles ok, but nothing ends up in my hashtable. Could somebody offer me
some advice on where im going wrong. Also i would like to retrieve this
data and display it? Would i use the .get method for this?

}
Block b[] = cc(motorModel);

MotorBike plane = new MotorBike(b, motorRegisNum, motorModel);
if (!motorBike.containsKey(motorRegisNum))
{
try {
motorBike.put(MotorRegisNum, motor);
What is "motor"? From the context, I would assume that you first
generate your "MotorBike" instance, then check whether a MotorBike with
the "motorRegisNum" is already in your hash, and if not, stuff your new
MotorBike in the hash. In that case, I would expect the last line
before my comment to read:
motorBike.put(MotorRegisNum, plane);
}
catch(Exception ex){
System.out.println("Problem Adding motorbike to a hash
table");
}
And? Do you get an exception? BTW, bad way to catch an exception...
 
T

Tuurbo46

Hi

Ere um. Im quite a newbee and its for some course work. I cannot really
post it all on here. would somebody be up for passing comment off group?

Hi Tuurbo,
another wild suggestion:
Im currently having a bit of a problem assesing a hashtable. The below
code
compiles ok, but nothing ends up in my hashtable. Could somebody offer me
some advice on where im going wrong. Also i would like to retrieve this
data and display it? Would i use the .get method for this?

}
Block b[] = cc(motorModel);

MotorBike plane = new MotorBike(b, motorRegisNum, motorModel);
if (!motorBike.containsKey(motorRegisNum))
{
try {
motorBike.put(MotorRegisNum, motor);
What is "motor"? From the context, I would assume that you first
generate your "MotorBike" instance, then check whether a MotorBike with
the "motorRegisNum" is already in your hash, and if not, stuff your new
MotorBike in the hash. In that case, I would expect the last line
before my comment to read:
motorBike.put(MotorRegisNum, plane);
}
catch(Exception ex){
System.out.println("Problem Adding motorbike to a hash
table");
}
And? Do you get an exception? BTW, bad way to catch an exception...
 
M

Monique Y. Mudama

MotorBike plane = new MotorBike(b, motorRegisNum, motorModel);

Something tells me that this variable name is poorly chosen. Do you
have a flying motorbike?
 
A

Andrew E

Tuurbo46 said:
Hi

Ere um. Im quite a newbee and its for some course work. I cannot really
post it all on here. would somebody be up for passing comment off group?

Not likely. You don't have to post it all. Just everything that lets people
understand your problem :)

Also, as a tip for a newbee, look up 'top-posting', and generally don't do it.
(I don't mind much myself, but I'm sure many feel differently)

Andrew
 
R

Roedy Green

MotorBike plane = new MotorBike(b, motorRegisNum, motorModel);
if (!motorBike.containsKey(motorRegisNum))
{
try {
motorBike.put(MotorRegisNum, motor);

you create plane and put motor. That does not make sense.
 
R

Roedy Green

MotorBike plane = new MotorBike(b, motorRegisNum, motorModel);
if (!motorBike.containsKey(motorRegisNum))

All the interesting code is missing.

Presumably somewhere you should have said

HashMap<String,MotorBike> motorBikes = new
HashMap<String,MotorBike>(1000);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top