Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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

<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>
like image 927
rozerro Avatar asked May 07 '26 17:05

rozerro


1 Answers

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.
like image 172
Matteo NNZ Avatar answered May 10 '26 07:05

Matteo NNZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!