Results 1 to 20 of 20
Like Tree3Likes
  • 1 Post By jim829
  • 2 Post By DarrylBurke

Thread: Setting a password + username, username works but getting errors for the password?

  1. #1
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Setting a password + username, username works but getting errors for the password?

    What am I doing wrong please?, username works perfectly, but when I enter password I get this error.
    Java Code:
    OKbtn.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent evt) {
    					if(Usernametxt.getText().equals("Admin") && 
    					(Passwordfld.getPassword().equals("Admin2"))){
    						System.out.println("permition granted");
    					}else{
    						System.out.println("permition Rejected");
    					}
    			
    					//else{}
    						
    				}
    permition Rejected
    permition Rejected
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MainMenu$2.actionPerformed(MainMenu.java:87)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Setting a password + username, username works but getting errors for the password

    First off, variable names should start with a lowercase letter. Failure to follow this convention only makes your code difficult for others to read

    Quote Originally Posted by PedramCarter View Post
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MainMenu$2.actionPerformed(MainMenu.java:87)
    So, what's on line 87 of MainMenu.java that hasn't been assigned a value?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Setting a password + username, username works but getting errors for the password

    The getPassword() method doesn't even return a String ... and the API documentation mumbles something about null being returned.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Setting a password + username, username works but getting errors for the password

    Quote Originally Posted by DarrylBurke View Post
    First off, variable names should start with a lowercase letter. Failure to follow this convention only makes your code difficult for others to read


    So, what's on line 87 of MainMenu.java that hasn't been assigned a value?

    db
    the passwordfld line

  5. #5
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Setting a password + username, username works but getting errors for the password

    Quote Originally Posted by JosAH View Post
    The getPassword() method doesn't even return a String ... and the API documentation mumbles something about null being returned.

    kind regards,

    Jos
    Can you tell me what to use instead? I tried to use getText that failed also.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Setting a password + username, username works but getting errors for the password

    Start by reading the API doc for the class that has the data you want to see what methods it has and how they work.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Setting a password + username, username works but getting errors for the password

    Quote Originally Posted by Norm View Post
    Start by reading the API doc for the class that has the data you want to see what methods it has and how they work.
    I had a look JPasswordField basic tutorial and examples, is there a more simple way of setting it similar to the way I was setting my username? I got a little confused because I'm using GUI builder and it's confusing the hell out of me.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Setting a password + username, username works but getting errors for the password

    You have to use the methods shown in the API doc.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Setting a password + username, username works but getting errors for the password

    A JPasswordField has its getText() method deprecated (as the docs tell you) for security reasons; you want to carry a password around in the open, as a String. A String can be created using a char array; combine the pieces ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Setting a password + username, username works but getting errors for the password

    Quote Originally Posted by JosAH View Post
    A JPasswordField has its getText() method deprecated (as the docs tell you) for security reasons; you want to carry a password around in the open, as a String. A String can be created using a char array; combine the pieces ...

    kind regards,

    Jos
    Quote Originally Posted by Norm View Post
    You have to use the methods shown in the API doc.
    I did it sort of it doesn't work :(

    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.JButton;
    import javax.swing.JEditorPane;
    import javax.swing.JLabel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Arrays;
    
    
    /**
    * This code was edited or generated using CloudGarden's Jigloo
    * SWT/Swing GUI Builder, which is free for non-commercial
    * use. If Jigloo is being used commercially (ie, by a corporation,
    * company or business for any purpose whatever) then you
    * should purchase a license for each developer using Jigloo.
    * Please visit www.cloudgarden.com for details.
    * Use of Jigloo implies acceptance of these licensing terms.
    * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
    * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
    * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
    */
    public class MainMenu extends javax.swing.JFrame {
    	private JLabel username;
    	private JLabel passwordlbl;
    	private JLabel Welco;
    	private JPasswordField Passwordfld;
    	private JButton Cancelbtn;
    	private JButton OKbtn;
    	private JTextField Usernametxt;
    	private String Password = "pass";
    
    	/**
    	* Auto-generated main method to display this JFrame
    	*/
    	
    	public static void main(String[] args) {
    		//PASSWORD field to store new password inside
    		//Passwordfld.setEchoChar('*');
    		//Passwordfld.addActionListener(new AL());
    		
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				MainMenu inst = new MainMenu();
    				inst.setLocationRelativeTo(null);
    				inst.setVisible(true);
    				
    			}
    		});
    	}
    	
    	public MainMenu() {
    		super();
    		initGUI();
    	}
    	private static boolean isPasswordCorrect(char[] input) {
    	    boolean isCorrect = true;
    	    char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };
    
    	    if (input.length != correctPassword.length) {
    	        isCorrect = false;
    	    } else {
    	        isCorrect = Arrays.equals (input, correctPassword);
    	    }
    
    	    //Zero out the password.
    	    Arrays.fill(correctPassword,'0');
    
    	    return isCorrect;
    	}
    	private void initGUI() {
    		try {
    			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    			getContentPane().setLayout(null);
    
    			username = new JLabel();
    			getContentPane().add(username);
    			username.setText("User Name:");
    			username.setBounds(63, 113, 80, 16);
    
    			passwordlbl = new JLabel();
    			getContentPane().add(passwordlbl);
    			passwordlbl.setText("Password:");
    			passwordlbl.setBounds(63, 160, 72, 16);
    
    			Usernametxt = new JTextField();
    			getContentPane().add(Usernametxt);
    			Usernametxt.setBounds(198, 110, 79, 23);
    
    			OKbtn = new JButton();
    			getContentPane().add(OKbtn);
    			OKbtn.setText("OK");
    			OKbtn.setBounds(102, 217, 59, 23);
    			OKbtn.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent evt) {
    				if(Usernametxt.getText().equals("Admin")){} /*&& 
    				(Passwordfld.getText().equals("Admin2"))*/
    					 
    					    String cmd = evt.getActionCommand();
    
    					    if (OKbtn.equals(cmd)) { //Process the password.
    					        char[] input = Passwordfld.getPassword();
    					        if (isPasswordCorrect(input)) {
    					            System.out.println("Password Accepted");
    					        } else {
    					           System.out.println("Password Rejected");
    					               
    					        }
    
    					        //Zero out the possible password, for security.
    					        Arrays.fill(input, '0');
    
    					        Passwordfld.selectAll();
    					        
    					
    						
    				/*else{
    						System.out.println("permition Rejected");
    					}*/
    			
    					//else{} 
    					/* if (Passwordfld = Password)= true) {
    						 System.out.println("permition granted");}*/
    					}
    				}
    			}
    			);
    
    			Cancelbtn = new JButton();
    			getContentPane().add(Cancelbtn);
    			Cancelbtn.setText("Exit");
    			Cancelbtn.setBounds(198, 217, 61, 23);
    
    			Welco = new JLabel();
    			getContentPane().add(Welco);
    			Welco.setText("Welcome to Jstore please log in using your staff acount");
    			Welco.setBounds(51, 19, 291, 16);
    
    			JPasswordField Passwordfld = new JPasswordField(10);
    			//Passwordfld.setText("Secret");
    			getContentPane().add(Passwordfld);
    			Passwordfld.setBounds(198, 157, 79, 23);
    			Passwordfld.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					//System.out.println("Passwordfld.actionPerformed, event="+evt);
    					//TODO add your code for Passwordfld.actionPerformed
    				}
    			});
    			
    			
    
    			pack();
    			setSize(400, 300);
    		} catch (Exception e) {
    		    //add your error handling code here
    			e.printStackTrace();
    		}
    		
    	}
    	//static class AL implements ActionListener{
    	//JPasswordField input = new JPasswordField();
    		
    	/*	char [] passy = input.getPassword();
    		String p = new String (passy);{
    		if (p.equals (Password)){
    			JOptionPane.showMessageDialog(null, "Correct"); }
    			else
    			{		JOptionPane.showMessageDialog(null, "InCorrect");
    				
    		}
    		*/
    				
    		
    	}
    Last edited by PedramCarter; 04-06-2015 at 05:37 AM.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Setting a password + username, username works but getting errors for the password

    Your class level JPasswordField is null, because you (locally) declared and initialized another one in your initGUI() method and it'll be gone when the method finishes ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  12. #12
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Setting a password + username, username works but getting errors for the password

    Quote Originally Posted by JosAH View Post
    Your class level JPasswordField is null, because you (locally) declared and initialized another one in your initGUI() method and it'll be gone when the method finishes ...

    kind regards,

    Jos
    No Idea what you mean can you copy the sections of my code as you explain so I know what you're talking about, sorry for troubling you and taking your time, btw I think I created other codes but I commented them out becaus they didn't work ignore them the part I just recently created the char one its coloured as if it's been commented no idea what it did that when I formatted and pasted my code in code style.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Setting a password + username, username works but getting errors for the password

    In your reply #10, remove the local (re)declaration of the JPasswordField in line #142.

    kind regards,

    Jod
    Build a wall around Donald Trump; I'll pay for it.

  14. #14
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Setting a password + username, username works but getting errors for the password

    Quote Originally Posted by JosAH View Post
    In your reply #10, remove the local (re)declaration of the JPasswordField in line #142.

    kind regards,

    Jod
    I removed line 142 and the jpasswordfield completely went i don't understand :'( I know I'm probably annoying sorry in advanced there is no re declaration that's the only instance?
    Last edited by PedramCarter; 04-06-2015 at 08:09 AM.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Setting a password + username, username works but getting errors for the password

    In line #30 you declared a JPasswordField (but never initialized it, so it is null) and in line #142 you declared (and initialized!) a local variable and you initialized it; don't (re)declare it in line #142, but only initialize the one you declared in line #30.

    kind regards,

    Jos (<-- can hardly spell his own name)
    Build a wall around Donald Trump; I'll pay for it.

  16. #16
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Setting a password + username, username works but getting errors for the password

    Quote Originally Posted by JosAH View Post
    In line #30 you declared a JPasswordField (but never initialized it, so it is null) and in line #142 you declared (and initialized!) a local variable and you initialized it; don't (re)declare it in line #142, but only initialize the one you declared in line #30.

    kind regards,

    Jos (<-- can hardly spell his own name)
    I never did that dude the gui builder does that

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Setting a password + username, username works but getting errors for the password

    Quote Originally Posted by PedramCarter View Post
    I never did that dude the gui builder does that
    OK, so you're innocent and you're stuck forever ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  18. #18
    PedramCarter is offline Member
    Join Date
    Nov 2014
    Posts
    27
    Rep Power
    0

    Default Re: Setting a password + username, username works but getting errors for the password

    thanks I fixed it.

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Setting a password + username, username works but getting errors for the password

    I recommend you stay away from GUI builders. It is best to use layout managers and have them place your components. And you will learn more in the process.

    Regards,
    Jim
    Deathslice likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Setting a password + username, username works but getting errors for the password

    Quote Originally Posted by jim829 View Post
    And you will learn more in the process.
    Mode correctly, you will learn *something* in the process.

    You don't learn by using a GUI builder if it generates code you can't even understand. Not even how to use the builder properly.

    db
    Deathslice and jim829 like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Username And Password
    By razmyasdfg in forum JDBC
    Replies: 9
    Last Post: 05-18-2011, 02:59 PM
  2. New to GUI. Logging in with Password and username
    By slitka in forum New To Java
    Replies: 3
    Last Post: 04-11-2011, 11:39 PM
  3. how to link url with username and password
    By gb.rashu in forum JavaServer Pages (JSP) and JSTL
    Replies: 13
    Last Post: 08-12-2010, 03:12 PM
  4. username password verification
    By bheezee in forum JDBC
    Replies: 0
    Last Post: 11-25-2008, 06:55 PM
  5. Help, created a username and password box
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •