Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

hashtable

Thread view: 
Tuurbo46 - 28 Nov 2005 17:51 GMT
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
cbroussard@liquiddatainc.com - 28 Nov 2005 18:09 GMT
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
Oliver Wong - 28 Nov 2005 18:13 GMT
> Hi
>
[quoted text clipped - 18 lines]
>
>        }

   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
pit.grinja@gmx.de - 28 Nov 2005 18:24 GMT
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
[quoted text clipped - 9 lines]
>             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...

>         }
Tuurbo46 - 28 Nov 2005 18:52 GMT
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:
[quoted text clipped - 26 lines]
>>
>>         }
Andrew E - 28 Nov 2005 20:53 GMT
> 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
Roedy Green - 29 Nov 2005 02:32 GMT
>  MotorBike plane = new MotorBike(b, motorRegisNum, motorModel);

what a strange name for a motorbike variable.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Monique Y. Mudama - 28 Nov 2005 18:56 GMT
>         MotorBike plane = new MotorBike(b, motorRegisNum, motorModel);

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

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Roedy Green - 29 Nov 2005 02:29 GMT
>        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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 29 Nov 2005 02:31 GMT
>        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);

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.