Error in actionPerformed?

I'm trying to create a button for importing CSV file and I'm getting this error:

actionPerformed(java.awt.event.ActionEvent) in  cannot implement 
    actionPerformed(java.awt.event.ActionEvent) in 
    java.awt.event.ActionListener; overridden method does not throw 
    java.io.IOException

public void actionPerformed(java.awt.event.ActionEvent evt) throws IOException {  
1 error

Code

import java.io.*;
import java.util.Arrays;
public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
    initComponents();
}

private void initComponents() {
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) throws IOException {
            jButton1ActionPerformed(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(130, 130, 130)
            .addComponent(jButton1)
            .addContainerGap(197, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(130, 130, 130)
            .addComponent(jButton1)
            .addContainerGap(147, Short.MAX_VALUE))
    );
    pack();
}

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) throws IOException {                                         
     String xfileLocation;

    xfileLocation="C:\\tra\\data1.txt";

    BufferedReader CSVFile = new BufferedReader(new FileReader(xfileLocation));
    try{
    String dataRow = CSVFile.readLine(); // Read the first line of data.
    // The while checks to see if the data is null. If it is, we've hit
    //  the end of the file. If not, process the data.
    while (dataRow != null){
        String[] dataArray = dataRow.split(",");
        for (String item:dataArray) { System.out.print(item + "\t"); }
        System.out.println(); // Print the data line.
        dataRow = CSVFile.readLine(); // Read next line of data.
    }

    // Close the file once all data has been read.
    CSVFile.close();

    // End the printout with a blank line.
    System.out.println();


     }
    catch(IOException e){
     }

}  

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}     

private javax.swing.JButton jButton1;
}                               
Jon Skeet
people
quotationmark

Well yes - look at the declaration of the method you're implementing:

void actionPerformed(ActionEvent e)

It isn't declared to throw any checked exceptions. So you can't add a checked exception such as IOException with a throws clause. You'll have to either handle the exception in your listener, or convert it to an unchecked exception and throw that.

(Your exception handling is already far from ideal - within the method, you catch IOException and swallow it completely silently... failing to close the file in that case. You should close the reader in a finally block or using a try-with-resources statement if you're using Java 7, and you should at least log the exception, potentially telling the user about it. Continuing as if nothing had gone wrong is a really bad idea.)

people

See more on this question at Stackoverflow