1

我遇到了错误:

java.lang.NoSuchMethodError: java.lang.String.isEmpty()Z

我的电脑上安装的java版本是1.8.0_91。

有趣的是,这个错误不会发生在我的电脑上,但在其他电脑上我试图运行我的程序。该错误似乎与通过 apache poi 4.1.1 从 excel 表中查找信息的类的一行有关。

令人不安的代码行是这一行:if(!CellContent.isBlank()){ 完整的类如下所示:

public class TrueExcelFind {



    XSSFSheet sheet;

    public TrueExcelFind() {

        try{

        String fileName = "C:/Temp/exceltest.xlsx";

        InputStream input = new FileInputStream(fileName);

        XSSFWorkbook wb = new XSSFWorkbook(input);
        sheet = wb.getSheetAt(0);

        } catch(FileNotFoundException ex) {
            System.err.println("File not found " + ex);
        } catch(IOException ex){
            System.err.println("Unable to load " + ex);
        }

    }

        private static int findRow(XSSFSheet sheet, String cellContent) {
            for (Row row : sheet) {
                for (Cell cell : row) {
                    if (cell.getCellType() == CellType.STRING) {
                        if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
                            return row.getRowNum();  
                        }
                    }

                    if (cell.getCellType().equals(CellType.NUMERIC)){
                        if (DateUtil.isCellDateFormatted(cell)) {
                            System.out.println(cell.getDateCellValue());

                        } else {
                            int lookup = (int) cell.getNumericCellValue();
                            String catcher = Integer.toString(lookup);

                            if(catcher.equals(cellContent)){
                                return row.getRowNum();
                            }
                        }

                    }

                }
            }               
            return 0;
        }

        public String getVaule(String suchobjekt, String ID){

            int colnr;
            int rownr;

            switch (suchobjekt) {
                case "Karton":
                    colnr = 10; 
                    break;
                case "Palette":
                    colnr = 11; 
                    break;
                default:
                    String ERROR = "ERROR, no such search-term";
                    return ERROR;
            }

            rownr = findRow(sheet, ID);

            XSSFRow row = sheet.getRow(rownr);
            XSSFCell cell = row.getCell(colnr);


            String CellContent = ""+cell;

            if(!CellContent.isBlank()){

                System.out.println("Outcome: "+row.getCell(colnr));
                return CellContent;

            } else {
                CellContent = "ERROR";
                System.out.println("Outcome: ERROR");
                return CellContent;   
            }


        }

}

我的程序做什么:我是另一个类,我正在尝试从文本字段中读取输入并检查 TrueExcelFind 类是否可以找到匹配的结果。如果是这样,打印出一个具体的答案。

我的猜测是该错误可能与 poi 库有关。这些库包含在可执行的 .jar 中。

有谁知道这里有什么问题?我被困在这一点上,不知道该怎么办。

4

1 回答 1

2

isEmpty 功能从 1.6 java 版本开始启用,可能在另一台电脑上安装了 java 5。

尝试在该电脑上运行 java -version 以丢弃它。

请记住,您始终可以使用本机验证,例如替换您的条件以在旧版本中运行:

if(!CellContent.isBlank()){

为了

 if(CellContent !=null || !"".equals(CellContent)){
于 2020-02-24T15:33:05.800 回答