请为我的作业提供帮助。 有人能告诉我为什么总有一个“NullPointerException”?
我共有5个课程,但我只发布了2个重要课程(GUI& InputDialog)
“Main”-GUI显示所有学生,当我点击“Add ...”按钮时,会出现InputDialog。但是当我点击“提交”时,会出现NullPointerException。为什么? 顺便说一下,我使用了ActionListener ......)
InputDialog为-GUI:
import javax.swing.JButton;
import javax.swing.JTextField;
public class InputDialog extends javax.swing.JFrame {
public InputDialog() {
initComponents();
jButton1.addActionListener(new GUI(jButton1));
}
public String getTfGeburtsdatum() {
return tfGeburtsdatum.getText();
}
public String getTfGeschlecht() {
return tfGeschlecht.getText();
}
public String getTfKatalognummer() {
return tfKatalognummer.getText();
}
public String getTfKlasse() {
return tfKlasse.getText();
}
public String getTfNachname() {
return tfNachname.getText();
}
public String getTfVorname() {
return tfVorname.getText();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
...
“主” -GUI:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
public class GUI extends javax.swing.JFrame implements ActionListener
{
private Model mod = new Model();
private Schueler sch;
private JButton button;
private InputDialog dia;
public GUI()
{
initComponents();
this.jList1.setComponentPopupMenu(this.jPopupMenu1);
this.showList();
}
public GUI(JButton button) {
this.button = button;
}
private void initComponents() {
...
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
dia = new InputDialog();
dia.setVisible(true);
}
private void showList()
{
FileBL bl = new FileBL();
bl.lesen();
mod = bl.getMod();
this.jList1.setModel(mod);
}
...
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == button){
String vn = dia.getTfVorname();
String nn = dia.getTfNachname();
String klasse = dia.getTfKlasse();
String katalognummer = dia.getTfKatalognummer();
String geschlecht = dia.getTfGeschlecht();
String geburtsdatum = dia.getTfGeburtsdatum();
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
Date gebDate = new Date();
try {
gebDate = sdf.parse(geburtsdatum);
} catch (ParseException ex) {
Logger.getLogger(InputDialog.class.getName()).log(Level.SEVERE, null, ex);
}
Schueler sch = new Schueler(vn,nn,klasse,katalognummer,geschlecht,gebDate);
mod.add(sch);
dia.setVisible(false);
this.jList1.setModel(mod);
}
}}
答案 0 :(得分:0)
我认为dia
是null
,因为dia
仅在私有方法中初始化
jButton3ActionPerformed
没有人打电话。
据我了解你的逻辑,你需要一个指向GUI实例的链接,该实例调用了这个对话框。例如:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
dia = new InputDialog(this);
dia.setVisible(true);
}
和
public class InputDialog extends javax.swing.JFrame {
public InputDialog(GUI caller) {
initComponents();
jButton1.addActionListener(caller);
}
....
因此,在这种情况下,caller
已初始化dia
,因为caller
调用了其私有方法jButton3ActionPerformed
并打开了此对话框。