1

I have encountered the error:

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

The installed java version on my pc is 1.8.0_91.

The funny thing is, that this error does not occur on my pc, but on other pc's I tried to run my program. The error seems to be connected to a line from a class who looks up info from an excel-sheet via apache poi 4.1.1.

The troubling line of code is this one: if(!CellContent.isBlank()){ the complete class looks like this:

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;   
            }


        }

}

What my program does: I an other class, I am trying to read the input from a text field and check if the class TrueExcelFind can find a matching result. If so, print out a specific answer.

My guess is that the error may have something to to with the poi libraries. The libraries are included in the executable .jar.

Does anyone have an idea what is wrong here? I am stuck at this point and don't know what to do.

Olaf Kock
  • 43,342
  • 7
  • 54
  • 84
Shathos
  • 25
  • 8
  • What is the installed Java-Version on the other PC? – Ben Feb 24 '20 at 15:30
  • 2
    `String.isEmpty()` was added in Java 1.6, so the JRE you are running on must be 1.5 or earlier. – Sean Bright Feb 24 '20 at 15:30
  • both pc's have version 1.8.0_91 – Shathos Feb 24 '20 at 15:49
  • `isBlank` is not available before Java version 11, but I wonder the error message being about `isEmpty` ?? – user85421 Feb 24 '20 at 16:03
  • I replaced `isBlank()` with `isEmpty && equals(" ")` and now it works fine for me. Wrong and correct inputs are identified correctly now. It seems like the isBlank() was what was causing the problem. I stil dont know why i got the `java.lang.NoSuchMethodError: java.lang.String.isEmpty()Z` error tho. – Shathos Feb 25 '20 at 08:54

1 Answers1

1

The isEmpty function is enabled since 1.6 java version, maybe in the other pc there is a java 5 installed.

Try to run a java -version in that pc to discard that.

And remeber you can always use native validation like replacing your condition to run in older versions :

if(!CellContent.isBlank()){

for

 if(CellContent !=null || !"".equals(CellContent)){
  • 1
    `isBlank()` was added in 11 and that's what he is using, not `isEmpry()` – mexicomanni Feb 24 '20 at 15:36
  • The other pc's have the same java version. But your suggestion kinda worked for me. BUT: Now, no result is found, instead of "Error", nothing is returned. Do you have an idea why this is the case now, and how I could fix this? :) Thanks for your help so far! – Shathos Feb 24 '20 at 15:46
  • Check if the sentence : XSSFCell cell = row.getCell(colnr); return an object, because based on what you tell me that sentence is returning null. and its a problem about the search in the doc. – Alejandro Venegas Feb 24 '20 at 17:56
  • It seems like I just needed to replace `!CellContent.isBlank()`. I replaced it with `!CellContent.isEmpty() && !CellContent.equals(" ")` and not my return works fine both ways. Thank you very much for putting me in the right direction! :) – Shathos Feb 25 '20 at 08:50