• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPasswordField

 
Dan Nissen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, Just having a bit of a problem when getting the contents of a JPasswordField. the cose im using is:
char[] PassTemp1 = (DPassword.getPassword());
Where DPassword is the JPasswordField.
For some reason when the program gets to this part of the code it stops ... maybe in a loop or something.
Can anyone shed some light on why it would be doing this? as far as I can tell this is the right code to get the contents of the JPasswordField.
Thanks
Dan
 
Dan Nissen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it helps I had a look in console and it would appear to be a nullpointer exception ... any help as to why I would be recieving that?
I am putting information into the JPasswordField.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bottom line is that somewhere you must be trying to use an identifier as if it pointed to an instantiated object, but you haven't actually instantiated the object yet (the identifier points to null).
What part of the code does the exception information specify?
It might be helpful if you were to post as much of your code as is relevant (which could be all of it) in order for others to best be able to help you figure this one out. Don't forget to surround your code with the [code] and [/code] UBB tags.
Good Luck.
 
Dan Nissen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok here is the code which is where the null pointer exception is occuring

the ** is where the null pointer exception is being generated.
[ code formatting edited -ds ]
[ June 18, 2002: Message edited by: Dirk Schreckmann ]
 
Mohan Panigrahi
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan,
Can you post your full code. It seems that DPassword is null, and I guess we need to see the full code to know why is that.
I guess that there must be some problem in declaring your action listener, ( of which this is the part, I suppose )

 
Dan Nissen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok sorry I didn't want to post my code because its pretty long but here it is.
By the way I really appreciate you guys helping me out (Thanks for the code formatting Dirk )

 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change
JPasswordField DPassword = new JPasswordField(20);
to
DPassword = new JPasswordField(20);
What you have done at that line is define DPassword as a local variable. You want to use the other reference.
 
Mohan Panigrahi
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan,
Paul's answer worked.
BUT, your program gives a stack of errors when you resize the applet to full.


I don't understand its cause.
Can any one throw some light.
Thanks
 
Dan Nissen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know about the resizing errors - I have the applets size set by my html file ... but I suspect it has something to do with my panels and their layout managers (I think 1 is wrong).
Next question about the code, Thanks Paul for your solution it worked however I am getting garbage from the JPassword field now instead of the text that I put in. Is the correct way to get the password from the field and turn the char[] to a string by using (DPassword.getPassword()).toString(); ? Because thats what im using.
Thanks again!
Dan
 
Vijay Venkat
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PasswordFieldChecker extends JFrame {
JPasswordField jTextField1 = new JPasswordField();
JPasswordField jTextField2 = new JPasswordField();
JButton jButton1 = new JButton();
public PasswordFieldChecker() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
PasswordFieldChecker passwordFieldChecker = new PasswordFieldChecker();
passwordFieldChecker.setSize(150,150);
passwordFieldChecker.setVisible(true);
}
private void jbInit() throws Exception {
jTextField1.setText("Field1");
jTextField2.setText("Field2");
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.getContentPane().add(jTextField1, BorderLayout.NORTH);
this.getContentPane().add(jTextField2, BorderLayout.SOUTH);
this.getContentPane().add(jButton1, BorderLayout.CENTER);
}
void jButton1_actionPerformed(ActionEvent e) {
char[] ch1 = jTextField1.getPassword();
char[] ch2 = jTextField2.getPassword();
String str1 = new String(ch1);
String str2 = new String(ch2);
System.out.println("is equal" + str1.equals(str2));
System.out.println(" str1 is :" +str1);
}
}
here is a sample code - which we tried to figure out similar problem and it worked fine.
Just try typng the password in the text field and click the button and you can see we get the Password !!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!