如何解决在Java中的java.lang.NoSuchFieldError: RETURN_NULL_AND_BLANK错误。

huangapple go评论77阅读模式
英文:

how to solve java.lang.NoSuchFieldError: RETURN_NULL_AND_BLANK in java

问题

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 = 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");
        }
    }
}

Here, ssa_msg_daily 表中包含空白或null值。在编译时,您可能会遇到以下错误:

Exception in thread "main" java.lang.NoSuchFieldError: RETURN_NULL_AND_BLANK

您可以如下处理此情况。我正在使用 Apache POI 将 ResultSet 转换为电子表格:

ResultSet resultSet = statement.executeQuery("select * from ssa_msg_daily");
XSSFWorkbook workbook = new XSSFWorkbook();
// ... (后续代码不变)

请注意,我已经将代码中的 HTML 实体引用(例如 ")更改为双引号(")来表示引号。这是因为在纯文本环境中,HTML 实体引用可能不适用。

英文:
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");
}
}
}

here ssa_msg_daily contains blank or null values in the table,so
while compiling i am getting error as

Exception in thread "main" java.lang.NoSuchFieldError: RETURN_NULL_AND_BLANK

occurs @ XSSFWorkbook workbook = new XSSFWorkbook();
how can i handle this situation and
i am using the resultset to convert to spreedsheet using apache poi

答案1

得分: 1

你遇到了一个类路径问题。在你的类路径上混合了不同版本的Apache POI项目。这些不同版本的混合导致一个较新版本的类尝试与一个较旧版本的类进行通信,而它们不兼容。

解决方案是检查你的类路径(所以,如果你使用Maven、Gradle或其他依赖系统,要查看依赖链),然后进行修复。可能只需在构建系统上运行一个“clean”命令就能解决。

注意:你的代码风格非常糟糕 - 不要将大量代码放在finally块中。此外,你的粘贴中99.9%都是不相关的。这一行代码已经引起了你的问题:

XSSFWorkbook workbook = new XSSFWorkbook();

其余部分都是无关紧要的。

英文:

You've got a classpath issue. You have a mix of versions of the Apache POI project on your classpath. This mix of versions is causing a newer version of one of the classes to try to talk to one of the older ones and they aren't compatible.

The solution is to check your classpath (so, if you're using maven, gradle, or some other dependency system, the chain of dependencies), and fix it up. Possibly it's as simple as running a 'clean' command on your build system.

NB: Your code is very bad style - don't put a ton of code in finally blocks. Furthermore, 99.9% of your paste is a red herring. This one-liner will cause your problem already:

XSSFWorkbook workbook = new XSSFWorkbook();

The rest is irrelevant.

答案2

得分: -1

或许您可以尝试使用 "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) {

        }

        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");
    }
}
英文:

Maybe you will try with "try" and "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");
}
}
}

huangapple
  • 本文由 发表于 2020年10月11日 20:46:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64304172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定