-4

I need help for my homework, please. Could someone tell me why there is always a "NullPointerException"?

I have altogether 5 classes but I only post the 2 important ones (GUI & InputDialog)

The "Main"-GUI shows all the students and when I click on the "Add..." button, the InputDialog appears. But when I click on "submit", there is a NullPointerException. Why? I used the ActionListener ... by the way :)

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() {
    ...

"Main"-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);
    }
}}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
TomBerry
  • 85
  • 1
  • 2
  • 9
  • 4
    please post the stacktrace of your exception. – nano_nano Mar 30 '15 at 14:47
  • 8
    I'm not sure anyone will want to read your whole code without a stacktrace to find a null pointer exception... I don't. – blue112 Mar 30 '15 at 14:47
  • 1
    something is not initialized properly, but it's hard to tell what from those bits of code. please show us the stacktrace – aniri Mar 30 '15 at 14:48
  • 1
    where is this.jList1 created? – nano_nano Mar 30 '15 at 14:48
  • my guess will be `dia` is null, but we could achieve more if you provide stack trace – user902383 Mar 30 '15 at 14:52
  • where you are handling the dialog submission. if your initial GUI is loading then there must be some issue with your dialog box code. Please add the stacktrace.. – Garry Mar 30 '15 at 14:54
  • 5
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – resueman Mar 30 '15 at 14:55
  • Please spend some time and learn how to use a debugger. Seriously. It's worth doing. – vanje Mar 30 '15 at 14:57

1 Answers1

0

I suppose that dia is null because dia is initialized only in a private method jButton3ActionPerformed which nobody calls.

As I understand your logic you need a link to the GUI instance which called this dialog. For example:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{                                         
    dia = new InputDialog(this);
    dia.setVisible(true);
}      

AND

public class InputDialog extends javax.swing.JFrame {

public InputDialog(GUI caller) {
    initComponents();

    jButton1.addActionListener(caller);
}

....

So in this case caller has dia initialized because caller called its private method jButton3ActionPerformed and opened this dialog.

phts
  • 3,889
  • 1
  • 19
  • 31