0

您好,我正在尝试制作一个程序,该程序允许我加载文件并将其名称上传到列表中。一旦我在列表中选择了一个文件名,它应该遍历该文件并将每一行放入指定的 jtextfield 中。但是当我尝试加载第二个文件并尝试选择它时,它告诉我arrayIndexOutOfBounds。有人可以向我解释我做错了什么。我正在使用 NetBeans。

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prog24178.assignment4;

import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

public class CustomerView extends javax.swing.JFrame {

    /**
     * Creates new form CustomerView
     */
    private Application ass4App = new Application();
    public ArrayList<Customer> customer = new ArrayList<Customer>();
    public ArrayList<String> names = new ArrayList<String>();
    public String fileName;
    public Customer customers = new Customer();
    public int i;

    public void setApplication(Application customerApp) {
        this.ass4App = ass4App;
    }

    public CustomerView() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */

    private void jExitItemActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        System.exit(0);
    }                                         

    private void jOpenCusItemActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        String currentPath = System.getProperty("user.dir");
        JFileChooser fc = new JFileChooser();

        fc.setMultiSelectionEnabled(true);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            File[] file = fc.getSelectedFiles();
            for (int i = 0; i < file.length; i++) {
                try {
                    customers.constructCustomer(file[i]);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(CustomerView.class.getName()).log(Level.SEVERE, null, ex);
                }
                customer.add(customers);
                names.add(customer.get(i).getName());
            }

            jCustomerList.setListData(names.toArray());            
        }
    }                                            

    private void jCustomerListValueChanged(javax.swing.event.ListSelectionEvent evt) {                                           
        // TODO add your handling code here:             
        jCusNameField.setText((String) customer.get(jCustomerList.getSelectedIndex()).getName());
        jAddressField.setText((String) customer.get(jCustomerList.getSelectedIndex()).getAddress());
        jCityField.setText((String) customer.get(jCustomerList.getSelectedIndex()).getCity());
        jProvinceField.setText((String) customer.get(jCustomerList.getSelectedIndex()).getProvince());
        jPostalCodeField.setText((String) customer.get(jCustomerList.getSelectedIndex()).getPostalCode());
        jEmailAddressField.setText((String) customer.get(jCustomerList.getSelectedIndex()).getEmailAddress());
        jPhoneNumberField.setText((String) customer.get(jCustomerList.getSelectedIndex()).getPhoneNumber());

    }             

我解决问题。我意识到我只是将可变客户添加到客户而没有给它一个适当的价值。customer.add(customers.constructCustomer(file[i]));

4

1 回答 1

0

我不知道具体是什么customers.constructCustomer(file[i]);customer.add(customers);做什么——我们没有足够的代码知道——但您正在使用 i 遍历 File 对象数组并获取客户 ( customers.get(i))。那是我要看的第二个地方。

我要看的第一个地方是错误消息;它告诉您数组索引超出范围的行、索引的值以及数组的上限。

于 2013-04-06T20:00:46.253 回答