Old 7th December 2005, 23:57   #1
k_rock923
\m/
(Forum King)
 
k_rock923's Avatar
 
Join Date: Jul 2003
Location: /bin/bash
Posts: 7,850
Send a message via AIM to k_rock923
Java help

I wrote a class for an input dialog for a word game assignment. It uses a password field so that the other player can't guess the sercret word as the first player types it in. However, my actionPerformed method in this class always throws an exception. Specifically, the getPassword() method does. I don't understand what I'm doing wrong. Any ideas?

code:

class CustomInputDialog extends JDialog implements ActionListener
{
private JLabel prompt;
private JPasswordField input;
private JButton ok;
private String userInput = new String("word");

public CustomInputDialog(Frame owner, String title, boolean modal)
{
super(owner, title, modal);
JPasswordField input = new JPasswordField(30);
input.setEchoChar('*');
JPanel screen = new JPanel(new FlowLayout());
ok = new JButton("OK");
prompt = new JLabel("Enter a word");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);

ok.addActionListener(this);

this.setModal(modal);
this.setResizable(false);

getContentPane().add(screen);

screen.add(prompt);
screen.add(input);
screen.add(ok);
pack();
}

public void actionPerformed(ActionEvent event)
{
char[] userString = null;
userString = input.getPassword();

StringBuffer temp = new StringBuffer();

for(int i = 0; i < userString.length; i++)
{
temp.append(userString[i]);
}

userInput = temp.toString();
JOptionPane.showMessageDialog(null,"here");
this.setVisible(false);
}

public String getText()
{
this.setVisible(true);
return userInput;
}

}



this is how I call it. I'm not sure if this is correct either. . .

code:

CustomInputDialog d = new CustomInputDialog(null, "Input", true);
word = d.getText();



word is a String instance variable of the class for the game UI. THanks in advance

//this was supposed to be in GD. oops.

[edit > JonnyMac] Now, it is in GD [/edit]

Never underestimate the bandwidth of a station wagon full of tapes hurtling down the highway.
k_rock923 is offline   Reply With Quote
Old 8th December 2005, 00:07   #2
sgtfuzzbubble011
 
sgtfuzzbubble011's Avatar
 
Join Date: Jun 2000
Posts: 60,839
Damn, you're quick JM. I didn't even get a chance to reply to his PM before it was moved. :P
sgtfuzzbubble011 is offline   Reply With Quote
Old 8th December 2005, 00:09   #3
k_rock923
\m/
(Forum King)
 
k_rock923's Avatar
 
Join Date: Jul 2003
Location: /bin/bash
Posts: 7,850
Send a message via AIM to k_rock923
Hey JM! nice to see you. how's tech support holding up? I really wish I could help out in there as much as I used to, but I've been really busy with college lately.

Never underestimate the bandwidth of a station wagon full of tapes hurtling down the highway.
k_rock923 is offline   Reply With Quote
Old 8th December 2005, 00:50   #4
JonnyMac
Moderator
 
JonnyMac's Avatar
 
Join Date: Dec 2000
Posts: 14,385
Hi sarge

I was looking in Who's Online and saw k_rock posting a new thread in TS.
k_rock posting a new thread in TS??? I It must be a case of temporary insanity.

Hi k_rock

WATS, has been ok lately and I have been taking it easy over there. No probs. I appreciate the help, but understand the need to devote more time to college. Good luck.

Please do not PM me for tech support. Any request for tech support through PM will be ignored.
Read the Stickies
---> | | | | <--- Knowledge is power
JonnyMac is offline   Reply With Quote
Old 8th December 2005, 09:16   #5
zootm
Forum King
 
zootm's Avatar
 
Join Date: Jan 2002
Location: the nether reaches of bonnie scotland
Posts: 13,375
What's the exception it throws?

zootm is offline   Reply With Quote
Old 8th December 2005, 14:17   #6
k_rock923
\m/
(Forum King)
 
k_rock923's Avatar
 
Join Date: Jul 2003
Location: /bin/bash
Posts: 7,850
Send a message via AIM to k_rock923
a NullPointer

however, it works when i do the following in the actionPerformed

JPasswordField input = (JPasswordField)e.getSource();
userString = input.getPassword();


now i'm very confused. I have no idea why i would need to do that.

Never underestimate the bandwidth of a station wagon full of tapes hurtling down the highway.
k_rock923 is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Community Center > General Discussions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump