Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compilation error on List.getFirst(): cannot find symbol?

Could you detect a problem of compilation? It compiles locally without any errors. But it fails when it was compiled using a distant autocode service over which I have no influence.

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /tmp/builds/autocode_133386/src/main/java/com/classes/ArrayRectangles.java:[76,46] cannot find symbol
  symbol:   method getFirst()
  location: variable perimeters of type java.util.List<java.lang.Double>
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.849 s
[INFO] Finished at: 2024-01-17T07:57:25Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project Classes: Compilation failure
[ERROR] /tmp/builds/autocode_133386/src/main/java/com/classes/ArrayRectangles.java:[76,46] cannot find symbol
[ERROR]   symbol:   method getFirst()
[ERROR]   location: variable perimeters of type java.util.List<java.lang.Double>

my code

List<Double> perimeters = new ArrayList<>();
...
double minPerim = perimeters.getFirst(); // error is here

pom file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <java.version>11</java.version>
    <junit5.version>5.8.2</junit5.version>
</properties>
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
    </configuration>
</plugin>

>Solution :

The method getFirst() has been added to the List interface starting from JDK 21.

It is highly possible that your code compiles locally because you’re using a JDK 21, but not on server because the server is using an image with an older JDK where the method is not yet declared.

Solutions:

  • (Long term): You upgrade your server to JDK 21
  • (Short term): Replace List.getFirst() by List.get(0), paying the right attention to when the list is empty (in that case, get(0) would raise an out of bound exception so you need to check the .size() first and return null (or throw an exception, as the current implementation of getFirst() does), if the list is empty.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading