最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Jan 29 at 12:37 Anonymous 86.3k15 gold badges161 silver badges177 bronze badges asked Jan 28 at 21:15 Sam TSam T 112 bronze badges 4
  • 1 What List do Nil and Cons actually implement? Should it be funlist.List or java.util.List ? – KompjoeFriek Commented Jan 28 at 22:03
  • Hello Sam T, welcome, none of your classes or interfaces have the closing keys, it is more than enough to cause a failure, if it is only an error when copying it to the site, I suggest you add the directory tree of your project. – Marce Puente Commented Jan 28 at 22:48
  • You have got import java.util.List; in Nil but not in Cons. It’s surely an error? And may be the reason for the error messages that you see. – Anonymous Commented Jan 29 at 8:35
  • Welcome to Stack Overflow. Ideas for further improvements of your question: (1) Explain in plain English how the relationships between your interface and your two classes were supposed to work. (2) Paste your error message into the question proper as text and format it as code to preserve line breaks and indentation. Don’t expect readers to follow links. – Anonymous Commented Jan 29 at 12:33
Add a comment  | 

1 Answer 1

Reset to default 0

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

  • What does a "Cannot find symbol" or "Cannot resolve symbol" error mean? (this question was earlier closed as a duplicate of that question)
  • print current date in java (about calling your own class the same as a library class)
发布评论

评论列表(0)

  1. 暂无评论