Why doesn't my log in button on my Swing GUI work?
-
I'm having some issues on this simple log in/password/username GUI class I made. I can type things into the text field easily, but when I press the log in button on my GUI, it fails, and gives me a big fat error. I've checked multiple sites and I can't seem to figure out what the problem is. I'll pose all my code and the error below. I also appreciate any additional feedback. Note: I know this seems like a Stack Overflow question, but they won't let me post questions there because I asked a question and a bunch of people flagged it. I tried fixing the question and I still haven't gotten anything. I'm also not an experienced enough programmer to really answer any questions, so I can't really raise my reputation at all. Here's the code for the GUI: package passwordProgram; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class LogInScreen implements ActionListener { JButton logIn; public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (UnsupportedLookAndFeelException e) { } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } LogInScreen logger = new LogInScreen(); logger.start(); } JFrame frame; JTextField username; JTextField password; public void start() { JFrame frame = new JFrame(); JPanel panel = new JPanel(); panel.setBackground(Color.RED); JButton logIn = new JButton("Log In"); logIn.addActionListener(this); JLabel title = new JLabel("Welcome to the Username/Password System"); JLabel usernameTxt = new JLabel("Username: "); JTextField username = new JTextField(15); JLabel passwordTxt = new JLabel("Password: "); JPasswordField password = new JPasswordField(15); frame.getContentPane().add(BorderLayout.CENTER, panel); frame.getContentPane().add(BorderLayout.SOUTH, logIn); frame.getContentPane().add(BorderLayout.NORTH, title); panel.setLayout(new GridBagLayout()); GridBagConstraints left = new GridBagConstraints(); left.anchor = GridBagConstraints.EAST; GridBagConstraints right = new GridBagConstraints(); right.weightx = (int) 2; right.fill = GridBagConstraints.HORIZONTAL; right.gridwidth = GridBagConstraints.REMAINDER; panel.add(usernameTxt, left); panel.add(passwordTxt, right); panel.add(username, right); panel.add(passwordTxt, left); panel.add(password, right); logIn.addActionListener(this); frame.setVisible(true); frame.setSize(500, 300); } public void actionPerformed(ActionEvent event) { if (password.getText().equals("foo")) { //test System.out.println("yes"); } else { System.out.println("No."); } } } And here's the error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at passwordProgram.LogInScreen.actionPerformed(LogInScreen.java:89) at javax.swing.AbstractButton.fireActionPerformed(Unknown 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.AbstractButton.doClick(Unknown Source) at javax.swing.plaf.basic.BasicRootPaneUI$Actions.actionPerformed(Unknown Source) at javax.swing.SwingUtilities.notifyAction(Unknown Source) at javax.swing.JComponent.processKeyBinding(Unknown Source) at javax.swing.KeyboardManager.fireBinding(Unknown Source) at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source) at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source) at javax.swing.JComponent.processKeyBindings(Unknown Source) at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown 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$000(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.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(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.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
-
Answer:
This is definitely a StackOverflow question. But I've seen that people answered programming questions here. So here is the problem: In your start() method you do not instantiate the fields that you created just before it (frame, username, password). Instead, you create new fields inside your method with the same name, and you think that they are referring to the same objects. But they are not. They are not even accessible outside of start() method. This causes a NullPointerException because "password" refer to null. One other problem is that you can not access password text simply by getText(). You should getPassword() and convert it into a String for comparison. I modified your code accordingly (see below). import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class LogInScreen implements ActionListener { JButton logIn; public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (UnsupportedLookAndFeelException e) { } catch (ClassNotFoundException e) { } catch (InstantiationException e) { } catch (IllegalAccessException e) { } LogInScreen logger = new LogInScreen(); logger.start(); } JFrame frame; JTextField username; JPasswordField password; public void start() { frame = new JFrame(); JPanel panel = new JPanel(); panel.setBackground(Color.RED); JButton logIn = new JButton("Log In"); logIn.addActionListener(this); JLabel title = new JLabel("Welcome to the Username/Password System"); JLabel usernameTxt = new JLabel("Username: "); username = new JTextField(15); JLabel passwordTxt = new JLabel("Password: "); password = new JPasswordField(15); frame.getContentPane().add(BorderLayout.CENTER, panel); frame.getContentPane().add(BorderLayout.SOUTH, logIn); frame.getContentPane().add(BorderLayout.NORTH, title); panel.setLayout(new GridBagLayout()); GridBagConstraints left = new GridBagConstraints(); left.anchor = GridBagConstraints.EAST; GridBagConstraints right = new GridBagConstraints(); right.weightx = (int) 2; right.fill = GridBagConstraints.HORIZONTAL; right.gridwidth = GridBagConstraints.REMAINDER; panel.add(usernameTxt, left); panel.add(passwordTxt, right); panel.add(username, right); panel.add(passwordTxt, left); panel.add(password, right); //logIn.addActionListener(this); frame.setVisible(true); frame.setSize(500, 300); } public void actionPerformed(ActionEvent event) { if (new String(password.getPassword()).equals("foo")) { //test System.out.println("yes"); } else { System.out.println("No."); } } }
Mustafa Zengin at Quora Visit the source
Related Q & A:
- Why doesn't my PHP function work as expected?Best solution by Stack Overflow
- Why doesn't the member search work anymore?Best solution by Yahoo! Answers
- Why doesn't it work when I want to click a link in my Yahoo mess list?Best solution by Yahoo! Answers
- Why doesn't my iphone lock button work?Best solution by Quora
- Why won't my print screen button work?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.