Question

This is my first program and now I am at a point i need help for a little problem.

    public void actionPerformed (ActionEvent ae){ 
    String name[] = new String[5];
        name[0] = "Nefarax";
        name[1] = "Gohanner";
        name[2] = "Tiradus";
        name[3] = "Musti";
        name[4] = "Tarik";
        int x;

    // Die Quelle wird mit getSource() abgefragt und mit den
    // Buttons abgeglichen. Wenn die Quelle des ActionEvents einer
    // der Buttons ist, wird der Text des JLabels entsprechend geändert
    if(ae.getSource() == this.button1){
        label.setText(("Anzahl der WP für Nefarax eintragen: "));
        buttonOK.addActionListener(this);
        panel.add(buttonOK);
        panel.add(tfName);
        buttonOK.setEnabled(true);
        tfName.setEnabled(true);
        x= 0;
    }
    else if(ae.getSource() == this.button2){
        label.setText("Button 2 wurde betätigt");
        buttonOK.setEnabled(true);
        tfName.setEnabled(true);
        buttonOK.addActionListener(this);
        panel.add(buttonOK);
        panel.add(tfName);
        x=1;
    }
    else if (ae.getSource() == this.button3){
        label.setText(("Button 3 wurde betätigt"));
        buttonOK.setEnabled(true);
        tfName.setEnabled(true);
        buttonOK.addActionListener(this);
        panel.add(buttonOK);
        panel.add(tfName);
        x=2;
    }

    else if (ae.getSource() == this.button4){
        label.setText(("Button 4 wurde betätigt"));
        buttonOK.setEnabled(true);
        tfName.setEnabled(true);
        buttonOK.addActionListener(this);
        panel.add(buttonOK);
        panel.add(tfName);
        x=3;
    }

    else if (ae.getSource() == this.button5){
        label.setText("Button 5 wurde betätigt");
        buttonOK.setEnabled(true);
        tfName.setEnabled(true);
        buttonOK.addActionListener(this);
        panel.add(buttonOK);
        panel.add(tfName);
        x=4;
    }

     if(ae.getSource() == this.buttonOK){

        label.setText(" ");
        buttonOK.setEnabled(false);
        tfName.setEnabled(false);

        save(name[x]);
     }   
    }
 public void save(String name)
{
    try
    {


        File file = new File("C:\\daten\\Wichtig\\" + name  +"Points.txt");
        if(!file.exists()) file.createNewFile();
        BufferedWriter writer=new BufferedWriter(new FileWriter(file));

        writer.write(tfName.getText()+ ";");

        writer.close();

    }

    catch(IOException exception)
    {
        JOptionPane.showMessageDialog(null, "Es ist ein Fehler beim Speichern aufgetreten." );
    }

}

}

save(name[x]) is not initiated but i don't know how?
I need this array to create .txt files with different names and for every button theres a new name

thanks

Was it helpful?

Solution

to initialize it, in the declaration, just set it to a certain value..
for example -1:

int x = -1;

or, optionally you can also use Integer instead of int

Integer x = null;

just remember before calling function abspeichern(), you also need to handle this further..
(this -1 or null value); unless it might cause an error

like:

if(ae.getSource() == this.buttonOK){

    label.setText(" ");
    buttonOK.setEnabled(false);
    tfName.setEnabled(false);

    if(x!=null){
        abspeichern(name[x]);
    }
 }   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top