1

I am currently using vscode and apache poi, created a program to automatically create a .xlsx program and have A1 cell input a String called "Tester", and that error pops up.

Codes in my program:

package excel_reader;

import java.io.FileOutputStream;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelWriter {
    public static void main (String[] args) throws Exception {
        //ExcelReader eR = new ExcelReader();
        XSSFWorkbook workbook = new XSSFWorkbook();  // here is the7 line 13
        // first sheet create
        XSSFSheet sheet = workbook.createSheet("FirstExcelSheet");
        // first row create - A
        XSSFRow row = sheet.createRow(0);
        // first cell create - 1
        XSSFCell cell = row.createCell(0); // A-1
        // give data into A-1 cell
        cell.setCellValue("Tester");

        // Output as an excel file
        workbook.write(new FileOutputStream("C:\\Users\\Sonic\\Desktop\\book.xlsx"));
        workbook.close();

        // C:\\Users\\Sonic\\Desktop\\book.xlsx
    }
}

Error Code:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell aka.ms/pscore6

PS E:\excel_reader_v.1_beta1-0> cd 'e:\excel_reader_v.1_beta1-0'; & 'C:\Users\Sonic\.vscode\extensions\vscjava.vscode-java-debug-0.24.0\scripts\launcher.bat' 'C:\Users\Sonic\AppData\Local\Programs\AdoptOpenJDK\bin\java' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Sonic\AppData\Local\Temp\cp_5wuvlu562pjj6rfd2pconxvl4.jar' 'excel_reader.ExcelWriter' 
Exception in thread "main" java.lang.NoSuchFieldError: DEFAULT
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:161)
        at excel_reader.ExcelWriter.main(ExcelWriter.java:13)

pom.xml (dependency):

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.11</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.1</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.6.1</version>
</dependency>

<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>3.1.0</version>
</dependency>

Apache POI is new to me, please do help me, I will be very appreciated, thank you very much.

FlashSonic526
  • 101
  • 6
  • 12
  • Can you please mark the line numbered `18` in your code (file `ExcelWriter.java`). This seems to be the line that causes the exception. – hc_dev Dec 26 '19 at 19:19
  • 2
    Any reason for extending ExcelReader? – Mehul Gayate Dec 26 '19 at 19:19
  • 1
    I just tried running your code. the only part I changed was removed Excel Reader entirely and then gave path to the file explicitly. It is working fine for me. – Brooklyn99 Dec 26 '19 at 19:35
  • @hc_dev mark the line numbered 18? May I ask what do you mean by marking line number 18? thank you, just confusing. – FlashSonic526 Dec 26 '19 at 20:00
  • @MehulGayate you right, for no reason, I should be double checking it first, but I wonder is it because of extending ExcelReader thus causing the exception? Thank you, I wish that I can understand and remember the mistake that I made to prevent having the same mistake next time, thank you. – FlashSonic526 Dec 26 '19 at 20:02
  • @FlashSonic526 I mean _mark the line_ numbered `18` __by a comment__ like `// here the error was`. And could you please __post the whole [stacktrace](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors)__ including lines starting with `Caused by:`. This helps spotting the error. Besides I have no clue what is class `ExcelReader` (your own class or from POI)? And what does method `getImpoFileLink()` do ? Maybe [edit] your question and post code of this class as well. – hc_dev Dec 26 '19 at 21:23
  • could you share the pom.xml in here? and also, it would be great if you could also put the result of `mvn dependency:tree` in here. – kucing_terbang Dec 29 '19 at 16:03
  • @kucing_terbang I updated the error code and other stuff, not sure about what is a `mvn dependency:tree`, but updated the error codes, codes, and added the dependency that i used, also, I do not know how to use a dependency or is that how I make a dependency, but I searched it online and that's what it said. – FlashSonic526 Dec 29 '19 at 17:58
  • @FlashSonic526 Have researched, found and totally revised my answer. Now I see your updated question (with POM, finally) - Great, you added everything! From your POM issue and solution are confirmed: only keep the 2 ``s with version `4.1.1` (i.e. `poi` and `poi-ooxml`) and remove the rest :-) – hc_dev Dec 29 '19 at 20:59
  • @hc_dev Umm, may you please teach me something about dependency? I still got the same error, and it still doesn't work for me, please do help me, thanks. ps. I am not sure about the dependency, I just created a file named `pom.xml`, and found all the dependency stuff thru this website, https://mvnrepository.com/artifact/org.apache.poi. – FlashSonic526 Dec 29 '19 at 21:23
  • @hc_dev btw, since I do not know much about dependency, therefore, I just follow the instructions where I put the `pom.xml` file into the `lib` folder, is that correct? I also wonder that is dependency = dependency injection? Thank you. – FlashSonic526 Dec 29 '19 at 21:24
  • @hc_dev Also, to make sure that I am following the steps correctly, the only 2 `dependency` I should have are the `poi` and `poi-ooxml`? Right? Also, I did not set a classpath to my `.xml` file, I just leave it in my `lib` folder, please correct me if I am not right, and also, I really appreciated for all who have been trying to help me resolving my problem, thank you. (I replyed to your message in the chat room, please check, tkx.) – FlashSonic526 Dec 29 '19 at 21:43
  • It said that my Maven project is `[Corrupted]` and when I click on it, it said, `Generating Dependency Tree: undefined` what does it mean? Please help and thanks. – FlashSonic526 Dec 29 '19 at 21:50

3 Answers3

12

Cause of java.lang.NoSuchFieldError

If you search StackOverflow for tag you will find a lot of similar questions. Most of them have 2 things in common:

This version mismatch may also cause your error:

java.lang.NoSuchFieldError: DEFAULT

Since the constant DEFAULT of class org.apache.poi.ss.formula.udf.UDFFinder existed in versions prior to 3.17, but now (versions 4.0 and newer) has been deprecated. Means, it does not exist in version 4.0 and above.

Problem at runtime: If statement new XSSFWorkbook(); (at line 13) is executed using dependency (JAR) poi-ooxml, then it uses a UDFFinder of the core dependency (JAR) poi and thus tries to find the CONSTANT field DEFAULT (which may not be there, since versions mismatch).

So I suppose, your versions of Apache poi and poi-ooxml are mismatching (not the same level). See similar question with version mismatch using Apache Poi.

Solution: matching versions!

If you are using Maven, make sure you have these dependencies on your POM:

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</version>
        </dependency>
    </dependencies>

At least both have to be the same version!

If you are managing your classpath and JARs manually, watch out for the filename of all apache-poi JAR files and make sure the versions are same.

Code seems very close to working

If you just want to write your freshly created Excel-Workbook to a file, e.g. /tmp/MyFirstExcel.xlsx (on Linux) or C:/Users/Sonic/MyFirstExcel.xlsx (on Windows, supposed your Windows-User is Sonic) then use this code:

public class ExcelWriter {
    static final String FILE_PATH_LINUX = "/tmp/MyFirstExcel.xlsx";
    static final String FILE_PATH_WIN = "C:/Users/Sonic/MyFirstExcel.xlsx"; // note: replace Window's backslash '\' by either double-backslash '\\' or single forward-slash '/'

    public static void main(String[] args) throws FileNotFoundException, IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("FirstExcelSheet"); // first sheet create
        XSSFRow row = sheet.createRow(0); // first row create - A
        XSSFCell cell = row.createCell(0); // first cell create - A-1        
        cell.setCellValue("Tester"); // give data into A-1 cell

        // Output as an excel file
        workbook.write(new FileOutputStream(FILE_PATH_WIN));
        workbook.close();
    }
}

Unclear usage of class ExcelReader: There is no obvious reason to extend from ExcelReader. Thus and for simplicity you can omit the extends ExcelReader, declaration of ExcelReader eR = new ExcelReader(); and usage eR.getImpoFileLink() to get the file-path. Otherwise please post the source-code of class ExcelReader to your question, to make it a minimal reproducible example.

Minimal working solution (above) explained: Instead just put a file-path to desired folder and file, like constant FILE_PATH_WIN here. I saw your related question few hours ago, where you did that.

Supposed you fixed your other issues/questions here, as well as resolved the version-mismatch with POI dependencies here, the solution given above should compile and write out the desired Excel workbook (.xlsx) with sheet FirstExcelSheet and cell A1 containing string Tester.

See also

See this tutorial on Apache POI – Reading and Writing Excel file in Java.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • I see, I thought that if I need to use the instance data (file path) from another class (ExcelReader, in the same package), I will have to make extend the class and make it to be a subclass thus I can use the instance data from it. – FlashSonic526 Dec 26 '19 at 20:06
  • I also wonder that if I want the program to group the same group of Strings (compare strings), but the only thing I can give is the column in excel file, is it possible to do that? I know that this seems confusing, I will explain it more detail if needed, but please do help me with it because I am stuck with it for a long time, thank you very much. – FlashSonic526 Dec 26 '19 at 20:09
  • @FlashSonic526 (1) Understood but need the full code of class `ExcelReader` see comment to your question. (2) Don't understand: `group the same group of Strings (compare strings)`. Maybe __add an example__ to your question, __better: ask in a new question__ and link both questions :-) – hc_dev Dec 26 '19 at 21:30
  • but the problem still does occur after removing `extend ExcelReader`, I put the file path directly to `workbook.write(new FileOutputStream(C:\\Users\\\\Desktop\\book1.xls`, it doesn't work, I put the link and store it into a constant String and use it, it still doesn't work, please do help me. – FlashSonic526 Dec 27 '19 at 07:46
  • I updated the entire stack trace, did not see anything that does has `Caused by:` but I updated the whoe stack trace, please take a look, thank you. – FlashSonic526 Dec 27 '19 at 07:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204959/discussion-between-hc-dev-and-flashsonic526). – hc_dev Dec 27 '19 at 18:24
0

Because putting this in the comment would not be feasible. Could you try to remove this portion of code in the pom.xml and try it again?

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.11</version>
</dependency>

based on cursory reading. it has been defined in the bellow code

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.1</version>
</dependency>
kucing_terbang
  • 4,991
  • 2
  • 22
  • 28
  • I supposed that you meant removing poi-ooxml 3.11 dependency, and keep poi-ooxml 4.1.1 dependency? – FlashSonic526 Dec 30 '19 at 09:10
  • yes. that's right :) seems like the field itself is not exists in 3.11 version – kucing_terbang Dec 30 '19 at 10:24
  • Sorry for the late response, I need some clarifying, what you meant "the field itself is not exists in 3.11 version", do you mean that by removing the `` only or remove the `.jar` file and dependency? Thanks. – FlashSonic526 Jan 02 '20 at 00:12
  • 1
    what I mean is you need to remove the `` tag. and afterward, you need to "package" your application again to ensure the aforementioned dependency has been removed from your `jar` file (so, by that extension, it would also remove the `.jar` file). – kucing_terbang Jan 02 '20 at 05:25
  • Thanks for your information, but just to let you know that since I do not know much about java and I only have 4 months experience with it, therefore, the dependency file was created by creating a new file in the `lib` folder and change the file type to `.pom`, so does these steps automatically add a "`` tab" to the package? – FlashSonic526 Jan 09 '20 at 14:15
  • well, it should not be. – kucing_terbang Jan 11 '20 at 10:20
0

The only apache-poi dependencies you need in pom.xml are

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.0</version>
</dependency>

Please remove others.

When I removed the others, my code worked.

likejudo
  • 3,396
  • 6
  • 52
  • 107