0

I am working on an Graphical User Interface, which uses two JTextFields(the third one shouldnt be relevant to this question) and a JComboBox. The selected Item from the ComboBox should decide which Textfield is added to the Arraylist "Gewínner" if the Button "Speichern" is pressed.

I am not allowed to use an inner class for the Listener.

Now the Problem: Whenever my ActionPerformed-method gets used i get an NullpointerException in the getGewinner-method. With the help of System.out.print I found that the null that gets pointed is the variable Winner in the call Winner.getSelectedItem().toString().

The Question: Why is Winner == null? Shouldnt it be initiated in the constructor? How can I change it ?

The Code In Question:

The GUI:

public class ErgebnisFrame extends JFrame{

private JLabel Spieler1, Spieler2, Gewinner, Punkte;
private JTextField one, two;
private JComboBox <String> Winner;
private JButton Abbruch, Speichern;

public ErgebnisFrame(){
    super();
    this.setVisible(true);
    this.setLayout(new GridLayout(5,2));
    this.setSize(300,400);  
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel Spieler1 = new JPanel();
    Spieler1.add(new JLabel("Name von Spieler 1"));
    this.add(Spieler1);

    JTextField one = new JTextField(1);
    this.add(one);

    JPanel Spieler2 = new JPanel();
    Spieler2.add(new JLabel("Name von Spieler 2"));
    this.add(Spieler2);

    JTextField two = new JTextField(1);
    this.add(two);

    JPanel Gewinner = new JPanel();
    Gewinner.add(new JLabel("Gewinner"));
    this.add(Gewinner);

    JComboBox<String> Winner = new JComboBox <String>();
    Winner.addItem("Spieler 1");
    Winner.addItem("Spieler 2");
    this.add(Winner);

    JPanel Punkte = new JPanel();
    Punkte.add(new JLabel("Punkte des Gewinners"));
    this.add(Punkte);

    JTextField points = new JTextField(1);
    this.add(points);

    JButton Abbruch = new JButton("Abbrechen");
    JButton Speichern = new JButton("Speichern");
    Speichern.setActionCommand("Speichern");
    Speichern.addActionListener(new SpeichernListener(this));
    this.add(Abbruch);
    this.add(Speichern);
}

public String getSpielerName(int Par){
    switch(Par){
        case 1: return one.getText();
        case 2: return two.getText();
        default: return "Es gibt nur zwei Spieler";     
    }
}

public String getSpieler1Name(){
    return one.getText();
}

public String getSpieler2Name(){
    return two.getText();
}

public String getGewinner(){ 
    return Winner.getSelectedItem().toString();
}

public static void main (String [] args){

    ErgebnisFrame Haupt = new ErgebnisFrame();


    }
}

the Listener:

public class SpeichernListener implements ActionListener {

private ErgebnisFrame Frame;
private static List <String> gewinner = new ArrayList <String>();

public SpeichernListener(ErgebnisFrame frame){
    this.Frame = frame;
}

public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("Speichern")){
        if(Frame.getGewinner().equals("Spieler 1")){
            gewinner.add(Frame.getSpieler1Name());
        }
        else{
            gewinner.add(Frame.getSpieler2Name());
        }
    }

}

public List <String> getGewinner(){
    return gewinner;
}


}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Warj
  • 5
  • 1
  • 4
  • 1
    See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Sep 16 '15 at 14:48
  • 2
    This statement `JComboBox Winner = new JComboBox ();` is redeclaring the `Winner` combo. as a local variable. Change it to `Winner = new JComboBox ();` .. – Andrew Thompson Sep 16 '15 at 14:50
  • 1
    Please conform to Java coding conventions: Type names (class,interface,enum) should start with a capital letter (e.g. `BigPicture`). Method, variable and field names should start with a lowercase letter (e.g. `bigPicture`), and constants should be all-caps (e.g. `BIG_PICTURE`). And it doesn't matter that the names are in German, it's still a convention that syntax highlighters, for example, expect. – RealSkeptic Sep 16 '15 at 14:52
  • Thanks for the answers. Andrew Thompsons comment answered my question. Sorry for it being a duplicate as I didnt found the other solution. And for the next post I will also pay more attention to the conventions. – Warj Sep 16 '15 at 15:05

0 Answers0