在我的班上,我有这些错误,我不知道如何摆脱它们,所以如果你愿意,我会非常感激:
public class MainGUI extends JFrame {
String username = "";
String password = "";
public MainGUI() {
initComponents();
}
private void initComponents() {
JLabel jLblLogin = new JLabel();
JTextField jTFUsername = new JTextField();
JLabel jLblUsername = new JLabel();
JLabel jLblPassword = new JLabel();
JButton jBtnLogin = new JButton();
JPasswordField jPFPassword = new JPasswordField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLblLogin.setFont(new java.awt.Font("Tahoma", 0, 24));
jLblLogin.setText("Login: ");
jLblUsername.setText("Username: ");
jLblPassword.setText("Password: ");
jBtnLogin.setText("Ok");
jBtnLogin.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBtnLoginActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLblLogin)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLblUsername)
.addComponent(jLblPassword))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTFUsername)
.addComponent(jBtnLogin, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPFPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE))))
.addContainerGap(70, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jLblLogin)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTFUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLblUsername))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLblPassword)
.addComponent(jPFPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(24, 24, 24)
.addComponent(jBtnLogin)
.addContainerGap(64, Short.MAX_VALUE)));
pack();
}
private void jBtnLoginActionPerformed(java.awt.event.ActionEvent evt) {
try {
Login tryLogin = new Login();
username = jTFUsername.getText();
password = jPFPassword.getText();
System.out.println(username + " " + password);
switch (tryLogin.verifyUser(username, password)) {
case "admin":
AdminFrame af = new AdminFrame();
af.setVisible(true);
this.setVisible(false);
break;
case "user":
UserFrame uf = new UserFrame();
uf.setVisible(true);
uf.passParameter(jTFUsername.getText());
this.setVisible(false);
break;
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MainGUI().setVisible(true);
}
});
}
private javax.swing.JButton jBtnLogin;
private javax.swing.JLabel jLblLogin;
private javax.swing.JLabel jLblPassword;
private javax.swing.JLabel jLblUsername;
private javax.swing.JPasswordField jPFPassword;
private javax.swing.JTextField jTFUsername;
}
错误如下:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PresentationLayer.MainGUI.jBtnLoginActionPerformed(MainGUI.java:88)
at PresentationLayer.MainGUI.access$000(MainGUI.java:15)
at PresentationLayer.MainGUI$1.actionPerformed(MainGUI.java:42)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
第88行:
username = jTFUsername.getText();
第16行:
public class MainGUI extends JFrame {
第42行:
jBtnLoginActionPerformed(evt);
我知道它是一个简单的nullpointerexception,但作为一个初学者,在尝试了一些解决方案后,我没有设法解决这个问题。
答案 0 :(得分:3)
问题在于您已在班级中声明jTFUsername
private javax.swing.JTextField jTFUsername;
但在实例化时,您在initComponents
方法中创建了另一个在本地创建小部件的方法
JTextField jTFUsername = new JTextField();
您需要实例化班级jTFUserName
才能使用。所以你需要在initComponents
方法中用
jTFUsername = new JTextField();
您的其他窗口小部件实例化也存在同样的问题。
答案 1 :(得分:1)
通过更改
尝试以下操作 private void initComponents() {
JLabel jLblLogin = new JLabel();
JTextField jTFUsername = new JTextField();
JLabel jLblUsername = new JLabel();
JLabel jLblPassword = new JLabel();
JButton jBtnLogin = new JButton();
JPasswordField jPFPassword = new JPasswordField();
到
private void initComponents() {
jLblLogin = new JLabel();
jTFUsername = new JTextField();
jLblUsername = new JLabel();
jLblPassword = new JLabel();
jBtnLogin = new JButton();
jPFPassword = new JPasswordField();