ArrayIndexOutOfBoundsException gui jlist

问题描述:

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

/* 
* 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]));

我不知道是什么customers.constructCustomer(file[i]);customer.add(customers);做的,正是 - 我们没有足够的代码就知道 - 但使用的是我通过File对象的数组迭代,并获得客户(customers.get(i)) 。这是我看的第二个地方。

我看的第一个地方是在错误消息;它会告诉您数组索引超出边界的行,索引的值以及数组的上限。