Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 处理文件的小程序在本地工作,但在网站中失败_Java_Swing_Applet_Swingworker_Indexoutofboundsexception - Fatal编程技术网

Java 处理文件的小程序在本地工作,但在网站中失败

Java 处理文件的小程序在本地工作,但在网站中失败,java,swing,applet,swingworker,indexoutofboundsexception,Java,Swing,Applet,Swingworker,Indexoutofboundsexception,这几天来我一直在讨论这个问题。我已经完成了我的谷歌搜索,我希望我能在这里找到一个比我更有经验的人(不难找到哈哈)来破解我的问题 场景:我开发了一个实现Swing GUI的Java小程序。后台工作:小程序从一个大的“电话簿”excel文件(.csv)中收集记录,并将其存储在地图数据结构中。电话簿包含大约106000条记录,在34586条记录中,我得到了一个我无法理解的ArrayIndexOutofBounds异常只有在我的个人网站上运行小程序时才会发生异常。在我的IDE(NetBeans)中测试并

这几天来我一直在讨论这个问题。我已经完成了我的谷歌搜索,我希望我能在这里找到一个比我更有经验的人(不难找到哈哈)来破解我的问题

场景:我开发了一个实现Swing GUI的Java小程序。后台工作:小程序从一个大的“电话簿”excel文件(.csv)中收集记录,并将其存储在地图数据结构中。电话簿包含大约106000条记录,在34586条记录中,我得到了一个我无法理解的ArrayIndexOutofBounds异常只有在我的个人网站上运行小程序时才会发生异常。在我的IDE(NetBeans)中测试并在本地计算机上运行.html文件(包含小程序的文件)时,小程序运行得非常好,没有错误。在我的网站上运行时引发的输出和异常如下(我剪切了大部分记录以节省空间):

Java控制台

如您所见,在34586条记录(从Patsy,Hart开始)上,它在她的地址中间输出。完整记录如下:Patsy,Hart,661 East 11th St.Apt.195,孟菲斯,田纳西州,38168555-555-5555

下面是受异常逻辑影响最大的代码部分

HTML文件中的对象标记


小程序无法运行。找不到Java插件。
人员数据库类(处理背景数据):

/*
 * Create a new record using an appropriately ordered set of fields and add it to the data base
 */
public void addRecordFromFields(String[] fields)
{
    // Read record attributes in, one at a time
    Record thisRecord = new Record();
    thisRecord.setFirstName(fields[0]);
    thisRecord.setLastName(fields[1]);
    thisRecord.setAddress(fields[2]);
    thisRecord.setCity(fields[3]);
    thisRecord.setState(fields[4]);
    thisRecord.setZipCode(fields[5]);
    thisRecord.setPhoneNo(fields[6]);
    addRecord(thisRecord);
}

// O( n )
/**
 * Destroy the current data base and load new data from a file.
 * @param filename the file to use as a source
 * @throws IOException: Either file not found or IO error
 */
public void initDBFromFile(URL url) throws IOException
{
    // Open and read the file
    InputStream in = url.openStream();
    BufferedReader filein = new BufferedReader(new InputStreamReader(in));
    // Read record file, parse lines, and add records to data base
    String line = filein.readLine();
    while(line != null) {
        System.err.println(line);
        String[] fields = line.split(",");
        addRecordFromFields(fields);
        line = filein.readLine();
    }
    filein.close();
}

/**
 * Loads the default library and provides for interaction with the data
 * via the JPanel GUI inputs.
 * @param args
 * @throws IOException 
 */
public String processData(String input, int selection, URL url)
{
    //Create the main library object
    PersonnelDatabase dbiLib = new PersonnelDatabase();
    System.err.println(url);
    // Try to read the default library
    try
    {
        dbiLib.initDBFromFile(url);
    }
    catch (IOException e)
    {
        System.err.println("File IO Error");
        e.printStackTrace();
        System.exit(1);
    }
    // Queries can be simulated by typing into the console in Eclipse, and using Ctrl-d (Ctrl-z in Windows) when finished.
    // For example: "searchLastName,Smith" would print a list of all people with the last name of Smith.
    Iterable<Record> result = null;
    String[] fields = new String[2];
    if (input.contains(",")) {
        fields = input.split(",");
    }

    switch(selection) {
        case 0: result = dbiLib.searchByFirstName(input); break;
        case 1: result = dbiLib.searchByLastName(input); break;
        case 2: result = dbiLib.searchByFullName(fields[0].trim(), fields[1].trim()); break;
        case 3: result = dbiLib.searchByCity(input); break;
        case 4: result = dbiLib.searchByState(input); break;
        case 5: result = dbiLib.searchByCityState(fields[0].trim(), fields[1].trim()); break;
        case 6: result = dbiLib.searchByZip(input); break;
        case 7: result = dbiLib.searchByPhoneNumber(input); break;
        case 8: String[] newFields = new String[fields.length-1];
                System.arraycopy(fields, 1, newFields, 0, fields.length-1);
                dbiLib.addRecordFromFields(newFields);
                return "Record added successfully!\nEnter a query or add another record.";
        default: return "Invalid query.\nEnter another query or add a record.";
    }
public void init() {
    /* Create and display the applet */
    try {
        java.awt.EventQueue.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                initComponents();
            }
        });
    } catch (Exception e) {
        System.err.println("Creation of GUI did not successfully complete.");
    }
}

// Process inputs in the background.
SwingWorker worker = new SwingWorker<String, Void>() {
    @Override
    public String doInBackground() {
        URL url = null;
        try {
            url = new URL(getCodeBase(), fileToRead);
        }
        catch(MalformedURLException e){}
        personnelDatabase = new PersonnelDatabase();
        final String output = personnelDatabase.processData(input, selection, url);
        return output;
    }

    @Override
    public void done() {
        processingLabel.setVisible(true);
        try {
            textToDisplay = get(15, TimeUnit.SECONDS);
        } catch (InterruptedException ignore) {
            ignore.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            String why = null;
            Throwable cause = e.getCause();
            if(cause != null) {
                why = cause.getMessage();
                cause.printStackTrace();
            } else {
                why = e.getMessage();
                e.printStackTrace();
            }
            System.err.println("Error retrieving request: " + why);
            }
        if(worker.isDone() && textToDisplay != null) {
            processingLabel.setVisible(false);
            outputTextArea.setText(textToDisplay);
        }
    }
};

private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
selection = searchComboBox.getSelectedIndex();
input = valueTextField.getText();
processingLabel.setVisible(true);

worker.execute();
/*
*使用一组有序的字段创建一个新记录,并将其添加到数据库中
*/
public void addRecordFromFields(字符串[]字段)
{
//在中读取记录属性,一次读取一个
Record thisRecord=新记录();
thisRecord.setFirstName(字段[0]);
thisRecord.setLastName(字段[1]);
thisRecord.setAddress(字段[2]);
thisRecord.setCity(字段[3]);
thisRecord.setState(字段[4]);
setZipCode(字段[5]);
thisRecord.setPhoneNo(字段[6]);
添加记录(此记录);
}
//O(n)
/**
*销毁当前数据库并从文件加载新数据。
*@param filename要用作源的文件
*@IOException:未找到文件或IO错误
*/
public void initDBFromFile(URL)引发IOException
{
//打开并读取文件
InputStream in=url.openStream();
BufferedReader filein=新的BufferedReader(新的InputStreamReader(in));
//读取记录文件,解析行,并将记录添加到数据库
String line=filein.readLine();
while(行!=null){
系统错误打印项次(行);
String[]fields=line.split(“,”);
addRecordFromFields(字段);
line=filein.readLine();
}
filein.close();
}
/**
*加载默认库并提供与数据的交互
*通过JPanel GUI输入。
*@param args
*@抛出异常
*/
公共字符串processData(字符串输入、int选择、URL)
{
//创建主库对象
PersonnelDatabase dbiLib=新的PersonnelDatabase();
System.err.println(url);
//尝试读取默认库
尝试
{
initDBFromFile(url);
}
捕获(IOE异常)
{
System.err.println(“文件IO错误”);
e、 printStackTrace();
系统出口(1);
}
//可以通过在Eclipse中的控制台中键入并在完成时使用Ctrl-d(Windows中的Ctrl-z)来模拟查询。
//例如:“searchLastName,Smith”将打印所有姓氏为Smith的人的列表。
Iterable result=null;
字符串[]字段=新字符串[2];
if(input.contains(“,”)){
字段=输入。拆分(“,”);
}
开关(选择){
案例0:result=dbiLib.searchByFirstName(输入);中断;
案例1:result=dbiLib.searchByLastName(输入);中断;
案例2:result=dbiLib.searchByFullName(字段[0].trim(),字段[1].trim());break;
案例3:result=dbiLib.searchByCity(输入);break;
案例4:result=dbiLib.searchByState(输入);break;
案例5:result=dbiLib.searchByCityState(字段[0].trim(),字段[1].trim());break;
案例6:result=dbiLib.searchByZip(输入);break;
案例7:result=dbiLib.searchByPhoneNumber(输入);中断;
案例8:String[]newFields=新字符串[fields.length-1];
System.arraycopy(字段,1,newFields,0,fields.length-1);
dbiLib.addRecordFromFields(新字段);
return“记录添加成功!\n输入一个查询或添加另一个记录。”;
默认值:返回“无效查询。\n输入另一个查询或添加记录。”;
}
PersonnelDatabaseApplet类(初始化GUI、收集输入并显示输出):

/*
 * Create a new record using an appropriately ordered set of fields and add it to the data base
 */
public void addRecordFromFields(String[] fields)
{
    // Read record attributes in, one at a time
    Record thisRecord = new Record();
    thisRecord.setFirstName(fields[0]);
    thisRecord.setLastName(fields[1]);
    thisRecord.setAddress(fields[2]);
    thisRecord.setCity(fields[3]);
    thisRecord.setState(fields[4]);
    thisRecord.setZipCode(fields[5]);
    thisRecord.setPhoneNo(fields[6]);
    addRecord(thisRecord);
}

// O( n )
/**
 * Destroy the current data base and load new data from a file.
 * @param filename the file to use as a source
 * @throws IOException: Either file not found or IO error
 */
public void initDBFromFile(URL url) throws IOException
{
    // Open and read the file
    InputStream in = url.openStream();
    BufferedReader filein = new BufferedReader(new InputStreamReader(in));
    // Read record file, parse lines, and add records to data base
    String line = filein.readLine();
    while(line != null) {
        System.err.println(line);
        String[] fields = line.split(",");
        addRecordFromFields(fields);
        line = filein.readLine();
    }
    filein.close();
}

/**
 * Loads the default library and provides for interaction with the data
 * via the JPanel GUI inputs.
 * @param args
 * @throws IOException 
 */
public String processData(String input, int selection, URL url)
{
    //Create the main library object
    PersonnelDatabase dbiLib = new PersonnelDatabase();
    System.err.println(url);
    // Try to read the default library
    try
    {
        dbiLib.initDBFromFile(url);
    }
    catch (IOException e)
    {
        System.err.println("File IO Error");
        e.printStackTrace();
        System.exit(1);
    }
    // Queries can be simulated by typing into the console in Eclipse, and using Ctrl-d (Ctrl-z in Windows) when finished.
    // For example: "searchLastName,Smith" would print a list of all people with the last name of Smith.
    Iterable<Record> result = null;
    String[] fields = new String[2];
    if (input.contains(",")) {
        fields = input.split(",");
    }

    switch(selection) {
        case 0: result = dbiLib.searchByFirstName(input); break;
        case 1: result = dbiLib.searchByLastName(input); break;
        case 2: result = dbiLib.searchByFullName(fields[0].trim(), fields[1].trim()); break;
        case 3: result = dbiLib.searchByCity(input); break;
        case 4: result = dbiLib.searchByState(input); break;
        case 5: result = dbiLib.searchByCityState(fields[0].trim(), fields[1].trim()); break;
        case 6: result = dbiLib.searchByZip(input); break;
        case 7: result = dbiLib.searchByPhoneNumber(input); break;
        case 8: String[] newFields = new String[fields.length-1];
                System.arraycopy(fields, 1, newFields, 0, fields.length-1);
                dbiLib.addRecordFromFields(newFields);
                return "Record added successfully!\nEnter a query or add another record.";
        default: return "Invalid query.\nEnter another query or add a record.";
    }
public void init() {
    /* Create and display the applet */
    try {
        java.awt.EventQueue.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                initComponents();
            }
        });
    } catch (Exception e) {
        System.err.println("Creation of GUI did not successfully complete.");
    }
}

// Process inputs in the background.
SwingWorker worker = new SwingWorker<String, Void>() {
    @Override
    public String doInBackground() {
        URL url = null;
        try {
            url = new URL(getCodeBase(), fileToRead);
        }
        catch(MalformedURLException e){}
        personnelDatabase = new PersonnelDatabase();
        final String output = personnelDatabase.processData(input, selection, url);
        return output;
    }

    @Override
    public void done() {
        processingLabel.setVisible(true);
        try {
            textToDisplay = get(15, TimeUnit.SECONDS);
        } catch (InterruptedException ignore) {
            ignore.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            String why = null;
            Throwable cause = e.getCause();
            if(cause != null) {
                why = cause.getMessage();
                cause.printStackTrace();
            } else {
                why = e.getMessage();
                e.printStackTrace();
            }
            System.err.println("Error retrieving request: " + why);
            }
        if(worker.isDone() && textToDisplay != null) {
            processingLabel.setVisible(false);
            outputTextArea.setText(textToDisplay);
        }
    }
};

private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
selection = searchComboBox.getSelectedIndex();
input = valueTextField.getText();
processingLabel.setVisible(true);

worker.execute();
public void init(){
/*创建并显示小程序*/
试一试{
java.awt.EventQueue.invokeAndWait(new Runnable()){
@凌驾
公开募捐{
初始化组件();
}
});
}捕获(例外e){
System.err.println(“GUI创建未成功完成”);
}
}
//在后台处理输入。
SwingWorker worker=新SwingWorker(){
@凌驾
公共字符串doInBackground(){
URL=null;
试一试{
url=新url(getCodeBase(),fileToRead);
}
捕获(格式错误){}
personnelDatabase=新的personnelDatabase();
最终字符串输出=personnelDatabase.processData(输入、选择、url);
返回输出;
}
@凌驾
公众假期结束(){
processingLabel.setVisible(true);
试一试{
textToDisplay=get(15,时间单位为秒);
}捕获(中断异常忽略){
ignore.printStackTrace();
}捕获(超时异常e){
e、 printStackTrace();
}捕获(执行例外){
字符串why=null;
    String[] fields = line.split(",");
    addRecordFromFields(fields);
Patsy,Hart,661 East 11th St.