Search code examples
javapackagecannot-find-symbol

How do I deal with "symbol not found" in my Java package?


My Java project for my CS class is acting completely whack, and I have no idea why. There's three classes (Cons, Nil, and List) all in a package called "funlist" and in the same folder (also named "funlist"). Despite this, they seem unable to recognize each other in their respective files. For example, the class declaration will say "class Cons implements List" and VSCode will say "symbol not found" where List is. However, it somehow still says "funlist.List" below the error, as though that isn't the symbol it's looking for.

cannot find symbol
  symbol:    class List
  localtion: package funlist (errors(1): 5:23-5:27) 
funlist.List

For further context on this, I downloaded the initial project files in the exact way I was supposed to from my college's site, and the problem is only happening on my computer as far as I'm aware. Here is the relevant code where these problems are cropping up:

package funlist;

import java.util.NoSuchElementException;

import static funlist.List.cons;
import static funlist.List.nil;


public final class Cons<T> implements List<T> {

package funlist;

import java.util.List;
import java.util.NoSuchElementException;

import static funlist.List.cons;

public final class Nil<T> implements List<T> {

package funlist;

sealed public interface List<T> permits Cons, Nil {

Furthermore, here are the fixes that have tried and failed so far:

  • Cleaning the Java workspace (several times)
  • Using other editors (such as IntelliJ)
  • Redownloading the files
  • Installing the exact version of Java my prof uses
  • Compiling into class files
  • Asking my prof for help

What makes it even more frustrating is that it sometimes works when submitting to Gradescope, but for reasons I cannot figure out. What do I do?


Solution

  • The problem

    One error message that I get in my IntelliJ IDEA is the following in Nil.java:

    Class 'Nil' must either be declared abstract or implement abstract method 'size()' in 'List'

    This is because you have imported java.util.List. Possibly your IDE offered you that as a fix to a different problem you had at that time. In any case the import causes Java to understand List as java.util.List, not your own List interface in the same package, which I believe you intended.

    I also get in List.java:

    Invalid permits clause: 'Nil' must directly implement 'List'

    Again this is because Nil implements java.util.List, not funlist.List.

    I can’t tell why you got “symbol not found”. Different IDEs and Java compilers do report errors differently. A possible explanation is that your tool failed to compile one of your class files, which caused a reference to that class or interface from another file to fail. That would typically be reported as “symbol not found” but would be nothing but a consequential error of the error in the other Java file, so not the one to focus on first. If nevertheless interested, you can read more about the “symbol not found” error message in the first link at the bottom.

    The fix

    Delete the line import java.util.List; from Nil.java. After I did this, I had no error message anymore.

    An even better fix may be to name your own interface something that isn’t also a name in the standard library. This would clear any confusion, and no IDE would suggest importing a standard interface that you never intended to use.

    Links to possibly related questions