簡體   English   中英

ArrayIndexOutOfBoundsException gui jlist

[英]ArrayIndexOutOfBoundsException gui jlist

您好,我正在嘗試制作一個程序,使我可以加載文件並將其名稱上傳到列表。 一旦我在列表中選擇了文件名,它就應該遍歷該文件,並將每一行放在指定的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]));

我不知道是什么customers.constructCustomer(file[i]); customer.add(customers); 確實可以-我們沒有足夠的代碼要知道-但是您正在使用i來遍歷File對象數組並獲得客戶( customers.get(i) )。 那是我要看的第二名。

我要看的第一處是錯誤消息。 它告訴您數組索引超出范圍的行,索引的值以及數組的上限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM