From: Tuurbo46 on
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






From: cbroussard on
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

From: Oliver Wong on

"Tuurbo46" <tuurbo46(a)yahoo.co.uk> wrote in message
news:dmfg2u$ojj$1(a)news8.svr.pol.co.uk...
> 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


From: pit.grinja on
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...
>
> }

From: Tuurbo46 on
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?

<pit.grinja(a)gmx.de> wrote in message
news:1133202285.512245.97760(a)g49g2000cwa.googlegroups.com...
> 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...
>>
>> }
>