Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: errors in H...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 6 Topic 16046 of 16590
Post > Topic >>

Re: errors in Hashtable to HashMap

by "Matt Humphrey" <matth@[EMAIL PROTECTED] > May 2, 2008 at 09:24 AM

"bH" <bherbst65@[EMAIL PROTECTED]
> wrote in message 
news:589c4c74-81c5-4e28-a36e-a18cf975064d@[EMAIL PROTECTED]
> Hi All,
>
> This is a scaled down version of the program:
> http://sio.midco.net/dfranklin/phonedial/index.html
>

<snip prelude>

> im****t java.applet.*;
> im****t java.awt.*;
> im****t java.util.HashMap;
>
> public class TestPhneDialer extends Applet {
>  boolean DEBUG = false;
>  HashMap buttonSoundNames;
^^^ You declare instance var. buttonSoundNames here, value is null

>  KeyPad keyPad;
>
>  public void init() {
>    keyPad = new KeyPad();
>    HashMap<Button,String>buttonSoundNames =
> new HashMap<Button,String>(3,3);
^^^ You create a local variable buttonSoundNames here, initialize it and 
insert values. But it is totally separate to the instance variable.  Drop 
the declaration part.

>
>    buttonSoundNames.put(keyPad.b1, "1.au");
>    buttonSoundNames.put(keyPad.b2, "2.au");
>    buttonSoundNames.put(keyPad.b3, "3.au");
>    setLayout(new FlowLayout());
>    add(keyPad);
>    validate();
>    System.out.println("Test line 1");
>    String btn3 = buttonSoundNames.get(keyPad.b3);
>    // will show "3.au"
>    System.out.println(btn3);
>  }
>
>  public boolean action(Event e, Object arg) {
>    if (e.target instanceof Button) {
>      System.out.println("Test line 2");
>      String name = new String((String)
> buttonSoundNames.get((Button)e.target));
^^^ You use the instance variable here, but it's value is null still.
Also, the new String() and casts are superfluous.  Just say
String name = buttonSoundNames.get(e.target).  You may find it clearer to 
keep the Button cast--I'm not sure which will be more readable.

Matt Humphrey http://www.iviz.com/
 




 6 Posts in Topic:
errors in Hashtable to HashMap
bH <bherbst65@[EMAIL P  2008-05-02 05:34:40 
Re: errors in Hashtable to HashMap
"Matt Humphrey"  2008-05-02 09:24:27 
Re: errors in Hashtable to HashMap
Roedy Green <see_websi  2008-05-02 14:33:09 
Re: errors in Hashtable to HashMap
bH <bherbst65@[EMAIL P  2008-05-02 08:13:11 
Re: errors in Hashtable to HashMap
"Matt Humphrey"  2008-05-02 12:58:05 
Re: errors in Hashtable to HashMap
bH <bherbst65@[EMAIL P  2008-05-02 16:38:33 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 16:03:57 CST 2008.