我是一个非常新的程序员,我目前正在创建一个程序,用作学校俱乐部的数据库。我目前在我的第一个JFrame上做了以下选项:
我的第一,第二,第三和第五帧在编译时工作正常。但是,编译我的第四个JFrame
表单时遇到问题。此表单用于添加新活动,并且有3个文本字段;一个用于活动名称,一个用于活动日期,一个用于活动出勤。还有一个按钮,显示"Add activity"
,它将新活动添加到Activity数组中。此按钮还会关闭第四个JFrame
,并将第一个JFRame
设置为true。
当我按下"Add activity"
按钮时,它会出现以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FourthForm.addActivityButtonActionPerformed(FourthForm.java:157)
at FourthForm.access$600(FourthForm.java:11)
at FourthForm$7.actionPerformed(FourthForm.java:79)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
以下是第四个JFrame
的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Username
*/
public class FourthForm extends javax.swing.JFrame {
private FirstForm first;
/**
* Creates new form FourthForm
*/
public FourthForm() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
activityNameTextField = new javax.swing.JTextField();
activityDateTextField = new javax.swing.JTextField();
activityAttendanceTextField = new javax.swing.JTextField();
addActivityButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(800, 600));
setResizable(false);
activityNameTextField.setText("Enter activity name here");
activityNameTextField.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
activityNameTextFieldMouseClicked(evt);
}
});
activityNameTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
activityNameTextFieldActionPerformed(evt);
}
});
activityDateTextField.setText("Enter date of activity here");
activityDateTextField.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
activityDateTextFieldMouseClicked(evt);
}
});
activityDateTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
activityDateTextFieldActionPerformed(evt);
}
});
activityAttendanceTextField.setText("Enter activity attendance here");
activityAttendanceTextField.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
activityAttendanceTextFieldMouseClicked(evt);
}
});
activityAttendanceTextField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
activityAttendanceTextFieldActionPerformed(evt);
}
});
addActivityButton.setText("Add activity");
addActivityButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addActivityButtonActionPerformed(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(77, 77, 77)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(activityNameTextField)
.addComponent(activityDateTextField)
.addComponent(activityAttendanceTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE))
.addComponent(addActivityButton, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(483, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(71, 71, 71)
.addComponent(activityNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addComponent(activityDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(37, 37, 37)
.addComponent(activityAttendanceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(54, 54, 54)
.addComponent(addActivityButton, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(273, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void activityNameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void activityDateTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void activityAttendanceTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void activityNameTextFieldMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
activityNameTextField.setText("");
}
private void activityDateTextFieldMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
activityDateTextField.setText("");
}
private void activityAttendanceTextFieldMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
activityAttendanceTextField.setText("");
}
private void addActivityButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String d = activityNameTextField.getText();
String e = activityDateTextField.getText();
String f = activityAttendanceTextField.getText();
int attendance = Integer.parseInt(f);
Activity a = new Activity(d, e, attendance);
first.addActivity(a);
this.setVisible(false);
first.setVisible(true);
}
/**
* @param args the command line arguments
*/
public void createActivity(FirstForm f)
{
activityNameTextField.setText(" ");
activityDateTextField.setText(" ");
activityAttendanceTextField.setText(" ");
this.setVisible(true);
first = f;
}
// public static void main(String args[]) {
// /* Set the Nimbus look and feel */
// //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
// /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
// * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
// */
// 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(FourthForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (InstantiationException ex) {
// java.util.logging.Logger.getLogger(FourthForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (IllegalAccessException ex) {
// java.util.logging.Logger.getLogger(FourthForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// } catch (javax.swing.UnsupportedLookAndFeelException ex) {
// java.util.logging.Logger.getLogger(FourthForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
// }
// //</editor-fold>
//
// /* Create and display the form */
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {7
//
// }
// });
// }
// Variables declaration - do not modify
private javax.swing.JTextField activityAttendanceTextField;
private javax.swing.JTextField activityDateTextField;
private javax.swing.JTextField activityNameTextField;
private javax.swing.JButton addActivityButton;
// End of variables declaration
}
我想要注意的是,此框架中使用的代码遵循与我的第二个jFrame
中的代码相同的结构,我在其中添加学生而不是活动。
如果我能对此有所了解,我会很感激!如果您需要我的任何其他JFrames
的代码,请询问。