如何解决java.lang.NoSuchFieldError:Java中的RETURN_NULL_AND_BLANK

问题描述

public class SpreadsheetGenerator {
void dailyreport( Connection con) throws sqlException,IOException {
    Statement statement = con.createStatement();
    ResultSet resultSet = null;
    try {
        resultSet = statement.executeQuery("select * from ssa_msg_daily");
    } catch (Exception e) {

    } finally {
        ResultSet resultSet = statement.executeQuery("select * from ssa_msg_daily");
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet spreadsheet = workbook.createSheet("employe db");

        XSSFRow row = spreadsheet.createRow(1);
        XSSFCell cell;
        cell = row.createCell(1);
        cell.setCellValue("id");
        cell = row.createCell(2);
        cell.setCellValue("Sender");
        cell = row.createCell(3);
        cell.setCellValue("Service");
        cell = row.createCell(4);
        cell.setCellValue("Message_identifier");
        cell = row.createCell(5);
        cell.setCellValue("Date");
        cell = row.createCell(6);
        cell.setCellValue("Incoming");
        cell = row.createCell(7);
        cell.setCellValue("Outgoing");
        int i = 2;

        while (resultSet.next()) {
            row = spreadsheet.createRow(i);
            cell = row.createCell(1);
            cell.setCellValue(resultSet.getInt("id"));
            cell = row.createCell(2);
            cell.setCellValue(resultSet.getString("Sender"));
            cell = row.createCell(3);
            cell.setCellValue(resultSet.getString("Service"));
            cell = row.createCell(4);
            cell.setCellValue(resultSet.getString("Message_identifier"));
            cell = row.createCell(5);
            cell.setCellValue(resultSet.getDate("Date"));
            cell = row.createCell(6);
            cell.setCellValue(resultSet.getString("Incoming"));
            cell = row.createCell(7);
            cell.setCellValue(resultSet.getString("Outgoing"));
            i++;
        }

        FileOutputStream out = new FileOutputStream(new File("exceldatabase.xlsx"));
        workbook.write(out);
        out.close();
        System.out.println("exceldatabase.xlsx written successfully");
    }
    }


}

此处ssa_msg_daily在表中包含空白或空值,因此
编译时我报错了

线程“ main”中的异常java.lang.NoSuchFieldError:RETURN_NULL_AND_BLANK

发生@ XSSFWorkbook工作簿= new XSSFWorkbook(); 我该如何处理这种情况? 我正在使用结果集使用apache poi转换为spreedsheet

解决方法

您遇到了类路径问题。您的类路径中混合了Apache POI项目的版本。版本的混合导致其中一个类的较新版本尝试与较旧的一个进行对话,并且它们不兼容。

解决方案是检查类路径(因此,如果您使用的是maven,gradle或其他依赖项系统,则依赖项链)并进行修复。可能就像在构建系统上运行“ clean”命令一样简单。

注意:您的代码风格很糟糕-不要在finally块中放入大量代码。此外,您的糊状物中99.9%是红色鲱鱼。这种单线将已经引起您的问题:

XSSFWorkbook workbook = new XSSFWorkbook();

其余无关紧要。

,

也许您会尝试使用“ try”和“ catch”。

public class SpreadsheetGenerator {
void dailyreport( Connection con) throws SQLException,IOException {
Statement statement = con.createStatement();
ResultSet resultSet = null;
try {
    resultSet = statement.executeQuery("select * from ssa_msg_daily");
} catch (Exception e) {

} 
    //ResultSet resultSet = statement.executeQuery("select * from ssa_msg_daily");
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet spreadsheet = workbook.createSheet("employe db");

    XSSFRow row = spreadsheet.createRow(1);
    XSSFCell cell;
    cell = row.createCell(1);
    cell.setCellValue("id");
    cell = row.createCell(2);
    cell.setCellValue("Sender");
    cell = row.createCell(3);
    cell.setCellValue("Service");
    cell = row.createCell(4);
    cell.setCellValue("Message_identifier");
    cell = row.createCell(5);
    cell.setCellValue("Date");
    cell = row.createCell(6);
    cell.setCellValue("Incoming");
    cell = row.createCell(7);
    cell.setCellValue("Outgoing");
    int i = 2;

    if(resultSet!=null){
    while (resultSet.next()) {
        row = spreadsheet.createRow(i);
        cell = row.createCell(1);
        cell.setCellValue(resultSet.getInt("id"));
        cell = row.createCell(2);
        cell.setCellValue(resultSet.getString("Sender"));
        cell = row.createCell(3);
        cell.setCellValue(resultSet.getString("Service"));
        cell = row.createCell(4);
        cell.setCellValue(resultSet.getString("Message_identifier"));
        cell = row.createCell(5);
        cell.setCellValue(resultSet.getDate("Date"));
        cell = row.createCell(6);
        cell.setCellValue(resultSet.getString("Incoming"));
        cell = row.createCell(7);
        cell.setCellValue(resultSet.getString("Outgoing"));
        i++;
    }}

    FileOutputStream out = new FileOutputStream(new File("exceldatabase.xlsx"));
    workbook.write(out);
    out.close();
    System.out.println("exceldatabase.xlsx written successfully");
}
}
 

}