I want to convert the input in a jpasswordfield to md5 but the problem is I don't know how
Printable View
I want to convert the input in a jpasswordfield to md5 but the problem is I don't know how
It's not difficult, you just need the magic incantation in Java.
Let me google that for you
MessageDigest is what you need. It looks a bit daunting at first, but most times you use it it'll be very little code: just a line or two.
Hello A4Andy,
I am going to assume you know how to get the value in the JPassowrdField already. As for converting that to md5, check out Message Digest.
too bad I don't know how to get the value of JPasswordField using JButton as the trigger. I already knew converting from String to md5 but not from JPasswordField to md5.
I tried this code but it didn't work
Code :public class HandlerClass1 implements ActionListener { public void actionPerformed(ActionEvent event) { char[] pass = pfPword.getPassword(); String s= new String(pass); MessageDigest m; try { m = MessageDigest.getInstance("MD5"); m.update(s.getBytes(),0,s.length()); System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16)); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
I'm getting a null pointer exception
Copy and paste it. You've read the API doc for getPasswordField and seen that it can throw a NPE?Quote:
I'm getting a null pointer exception
Code :Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at db$HandlerClass1.actionPerformed(db.java:100) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:253) at java.awt.Component.processMouseEvent(Component.java:6203) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:5968) at java.awt.Container.processEvent(Container.java:2105) at java.awt.Component.dispatchEventImpl(Component.java:4564) at java.awt.Container.dispatchEventImpl(Container.java:2163) at java.awt.Component.dispatchEvent(Component.java:4390) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4461) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4125) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4055) at java.awt.Container.dispatchEventImpl(Container.java:2149) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4390) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649) at java.awt.EventQueue.access$000(EventQueue.java:96) at java.awt.EventQueue$1.run(EventQueue.java:608) at java.awt.EventQueue$1.run(EventQueue.java:606) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116) at java.awt.EventQueue$2.run(EventQueue.java:622) at java.awt.EventQueue$2.run(EventQueue.java:620) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105) at java.awt.EventQueue.dispatchEvent(EventQueue.java:619) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177) at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
I input test in the passwordfield
There's not much in there that could be throwing an NPE. Which is line 100 in db.java? If you've edited the file since you posted the stack trace, make sure you select the line on which the Exception is thrown in your latest source.
Code :public class HandlerClass1 implements ActionListener { public void actionPerformed(ActionEvent event) { 100 char[] pass = pfPword.getPassword(); 101 String s= new String(pass); 102 MessageDigest m; 103 try { 104 m = MessageDigest.getInstance("MD5"); 105 m.update(s.getBytes(),0,s.length()); 106 System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16)); 107 } catch (NoSuchAlgorithmException e) { 108 // TODO Auto-generated catch block 109 e.printStackTrace(); 110 } } }
Why is pfPword null? Isn't that your password control? I'm guessing it's not the NPE that the API doc warns about in JPasswordField.getPassword() - or the stack trace should start inside that method. Printing the value of pfPword before line 100 would show for sure.
Here is my whole db class
Code :import java.awt.*; import java.awt.event.*; import java.security.*; import java.math.*; import javax.swing.*; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; public class db extends JPanel { /** * */ private static final long serialVersionUID = 1L; private JLabel lbldbURL,lbldbName,lbldbUName,lbldbPword; private JTextField txtdbURL,txtdbName,txtdbUname; private JPasswordField pfPword; private JButton saveButton; public db() { this.setBackground(Color.WHITE); setLayout(null); Font serif = new Font("serif",Font.PLAIN,14); //////////// //Labels //////////// // //Database URL JLabel lbldbURL = new JLabel("Database URL:"); lbldbURL.setBounds(290, 110, 170, 20); lbldbURL.setFont(serif); add(lbldbURL); // //Database Name JLabel lbldbName = new JLabel("Database Name:"); lbldbName.setFont(serif); lbldbName.setBounds(280, 150, 170, 20); add(lbldbName); // //Database User name JLabel lbldbUName = new JLabel("Database Username:"); lbldbUName.setFont(serif); lbldbUName.setBounds(250, 190, 170, 20); add(lbldbUName); // //Database Password JLabel lbldbPword = new JLabel("Database Password:"); lbldbPword.setFont(serif); lbldbPword.setBounds(255, 230, 170, 20); add(lbldbPword); //////////////// //text field /////////////// // //text database URL JTextField txtdbURL = new JTextField(); txtdbURL.setFont(serif); txtdbURL.setBounds(410, 110, 170, 20); add(txtdbURL); // //text database name JTextField txtdbName = new JTextField(); txtdbName.setFont(serif); txtdbName.setBounds(410, 150, 170, 20); add(txtdbName); // //Database User textfield JTextField txtdbUName = new JTextField(); txtdbUName.setFont(serif); txtdbUName.setBounds(410, 190, 170, 20); add(txtdbUName); // //password field database password JPasswordField pfPword = new JPasswordField(); pfPword.setFont(serif); pfPword.setBounds(410, 230, 170, 20); add(pfPword); // //Save Button JButton saveButton = new JButton("Save"); saveButton.setBounds(410, 260, 170, 20); add(saveButton); //////////// //Action Listener //////////// HandlerClass1 handler1 = new HandlerClass1(); saveButton.addActionListener(handler1); } static class AL implements ActionListener{ public void actionPerformed(ActionEvent event) { } } public class HandlerClass1 implements ActionListener { public void actionPerformed(ActionEvent event) { char[] pass = pfPword.getPassword(); String s= new String(pass); MessageDigest m; try { m = MessageDigest.getInstance("MD5"); m.update(s.getBytes(),0,s.length()); System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } } }
Heheh - there's another thread on the forum today about this. How many times have you declared pfPword?
what do you mean I think only once.
search your text file for "JPasswordField". It should be in there only twice - once when you declare the reference pfPword and once when you use the JPasswordField constructor. I count more than two in your 'whole db class' post:
http://www.javaprogrammingforums.com...html#post42561
it's now working well
Code :import java.awt.*; import java.awt.event.*; import java.security.*; import java.math.*; import javax.swing.*; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; public class db extends JPanel { /** * */ private static final long serialVersionUID = 1L; private JLabel lbldbURL,lbldbName,lbldbUName,lbldbPword; private JTextField txtdbURL,txtdbName,txtdbUname; [COLOR="#FF0000"] private JPasswordField pfPword = new JPasswordField();[/COLOR] private JButton saveButton; public db() { this.setBackground(Color.WHITE); setLayout(null); Font serif = new Font("serif",Font.PLAIN,14); //////////// //Labels //////////// // //Database URL JLabel lbldbURL = new JLabel("Database URL:"); lbldbURL.setBounds(290, 110, 170, 20); lbldbURL.setFont(serif); add(lbldbURL); // //Database Name JLabel lbldbName = new JLabel("Database Name:"); lbldbName.setFont(serif); lbldbName.setBounds(280, 150, 170, 20); add(lbldbName); // //Database User name JLabel lbldbUName = new JLabel("Database Username:"); lbldbUName.setFont(serif); lbldbUName.setBounds(250, 190, 170, 20); add(lbldbUName); // //Database Password JLabel lbldbPword = new JLabel("Database Password:"); lbldbPword.setFont(serif); lbldbPword.setBounds(255, 230, 170, 20); add(lbldbPword); //////////////// //text field /////////////// // //text database URL JTextField txtdbURL = new JTextField(); txtdbURL.setFont(serif); txtdbURL.setBounds(410, 110, 170, 20); add(txtdbURL); // //text database name JTextField txtdbName = new JTextField(); txtdbName.setFont(serif); txtdbName.setBounds(410, 150, 170, 20); add(txtdbName); // //Database User textfield JTextField txtdbUName = new JTextField(); txtdbUName.setFont(serif); txtdbUName.setBounds(410, 190, 170, 20); add(txtdbUName); // //password field database password pfPword.setBounds(410, 230, 170, 20); add(pfPword); // //Save Button JButton saveButton = new JButton("Save"); saveButton.setBounds(410, 260, 170, 20); add(saveButton); //////////// //Action Listener //////////// HandlerClass1 handler1 = new HandlerClass1(); saveButton.addActionListener(handler1); } static class AL implements ActionListener{ public void actionPerformed(ActionEvent event) { } } public class HandlerClass1 implements ActionListener { public void actionPerformed(ActionEvent event) { char[] pass = pfPword.getPassword(); String s= new String(pass); MessageDigest m; try { m = MessageDigest.getInstance("MD5"); m.update(s.getBytes(),0,s.length()); System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } } }
My code before was actually a bad practice?
Glad you got it working. It's not so much 'bad practice' as just wrong. You declared two references to JPasswordField, only one of which you assigned to a JPasswordField object. The other one's value is by default null. It's quite a common source of error - the other guys were talking about it here yesterday as 'shadowing' - more than one variable with the same name. The two variables may have completely different scope (where they can be 'seen' from in the code), or may even overlap. The compiler decides which one you mean (usually the most *local* one), but you may have last assigned to the other one ... boom.
Not always easy to spot when you've done it either. If you find yourself thinking "that's not null, I just set it there...", it's worth checking to see if you've declared the reference more than once.