Re: errors in Hashtable to HashMap
"bH" <bherbst65@hotmail.com> wrote in message
news:589c4c74-81c5-4e28-a36e-a18cf975064d@l42g2000hsc.googlegroups.com...
Hi All,
This is a scaled down version of the program:
http://sio.midco.net/dfranklin/phonedial/index.html
<snip prelude>
import java.applet.*;
import java.awt.*;
import 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/