Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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 使用opencsv时ArrayIndexOutOfBoundsException错误_Java_Opencsv - Fatal编程技术网

Java 使用opencsv时ArrayIndexOutOfBoundsException错误

Java 使用opencsv时ArrayIndexOutOfBoundsException错误,java,opencsv,Java,Opencsv,我希望有人能告诉我为什么会收到错误:“线程中的异常”main“java.lang.ArrayIndexOutOfBoundsException:1” 我正在使用opencsv从csv中插入数据,代码确实有效,因为数据按预期插入到表中。然而,我得到了错误 下面是我的代码: PreparedStatement sql_statement = null; String jdbc_insert_sql = "INSERT INTO wifi_users(email, f

我希望有人能告诉我为什么会收到错误:“线程中的异常”main“java.lang.ArrayIndexOutOfBoundsException:1”

我正在使用opencsv从csv中插入数据,代码确实有效,因为数据按预期插入到表中。然而,我得到了错误

下面是我的代码:

    PreparedStatement sql_statement = null;         
    String jdbc_insert_sql = "INSERT INTO wifi_users(email, first_name, last_name, gender, birthday, opted_in) VALUES (?, ?, ?, ?, ?, ?)";
    sql_statement = conn.prepareStatement(jdbc_insert_sql);

    /* Read CSV file in OpenCSV */
    String inputCSVFile = "/Users/Gerry/Documents/workspace/sql_out_connection/users.csv";
    CSVReader reader = new CSVReader(new FileReader(inputCSVFile));

    /* Variables to loop through the CSV File */
    String [] nextLine; /* for every line in the file */ 
    int lnNum = 0; /* line number */
    while ((nextLine = reader.readNext()) != null) {
            lnNum++;                    
            sql_statement.setString(1,(nextLine[0]));
            sql_statement.setString(2,(nextLine[1]));
            sql_statement.setString(3,(nextLine[2]));
            sql_statement.setString(4,(nextLine[3]));
            sql_statement.setString(5,(nextLine[4]));
            sql_statement.setDouble(6,Double.parseDouble(nextLine[5]));
            /* execute the insert statement */
            sql_statement.executeUpdate();
    }   

有人知道我为什么会出现此错误吗?

您的CSV文件中有一行的值小于预期的最小值6(0-5),因此出现ArrayIndexOutOfBoundsException

在继续数据库更新之前,您可能需要验证是否需要6个值

顺便说一句,你不需要在你的值周围加括号。最好这样

sql_statement.setString(1, nextLine[0]);

CSV文件中的一行小于预期的最小值6(0-5),因此存在ArrayIndexOutOfBoundsException

在继续数据库更新之前,您可能需要验证是否需要6个值

顺便说一句,你不需要在你的值周围加括号。最好这样

sql_statement.setString(1, nextLine[0]);

谢谢你,马诺。您是正确的,有一行在两个值之间缺少逗号,因此被解释为5而不是6.5。很高兴为你修好了。那么,您愿意将此标记为您的答案吗?:)谢谢你,马诺。您是正确的,有一行在两个值之间缺少逗号,因此被解释为5而不是6.5。很高兴为你修好了。那么,您愿意将此标记为您的答案吗?:)