1

I have the following classes:

List.java:

package list;
public class List<T> {

private Node<T> first = null;

    public List () {
        this.first = new Node<T>(null);
    }
}

Node.java:

package list;
public class Node<T> {

    T data;
    Node<T> next = null;

    public Node(T t) {
        this.data = t;
    }
}

I'm getting these errors while trying to compile:

List.java:17: error: cannot find symbol
    private Node<T> first;
            ^
  symbol:   class Node
  location: class List<T>
  where T is a type-variable:
    T extends Object declared in class List

List.java:25: error: cannot find symbol
        this.first = new Node<T>(null);
                         ^
  symbol:   class Node
  location: class List<T>
  where T is a type-variable:
    T extends Object declared in class List

What am I missing?

Lior
  • 5,841
  • 9
  • 32
  • 46
  • 1
    Can you describe the file structure of your application? Do you import `Node` in your `List` class? The error you show doesn't really relate to generics, java is saying that it cannot work out what you mean by `Node` – cameron1024 Jan 21 '20 at 21:48
  • No, I'm not importing Node. But I've put the two classes in the same package. – Lior Jan 21 '20 at 21:48
  • 1
    Have you compiled `Node`? – khelwood Jan 21 '20 at 21:49
  • If your Node class is located in a different package, you have to import it. Normally the IDE helps you with problems like that. – Moritz Schmidt Jan 21 '20 at 21:50
  • Yes, I have a Node.class file in the same folder. – Lior Jan 21 '20 at 21:50
  • @MoritzSchmidt Both of the classes are in the same package – Lior Jan 21 '20 at 21:50
  • How do you compile your code? – Moritz Schmidt Jan 21 '20 at 21:51
  • 2
    Since both classes are in package `list`, both java source files must be in folder `list` and the classpath / sourcepath must be the parent folder of the `list` folder. Is that the case? – Andreas Jan 21 '20 at 21:51
  • @MoritzSchmidt using javac List.java and javac Node.java – Lior Jan 21 '20 at 21:52
  • I'm not an expert in this, but perhaps it is something to do with Java 9 modules preventing your `Node` class being visible to `List` – cameron1024 Jan 21 '20 at 21:52
  • @Andreas No, they are just in a folder called "Assignment" – Lior Jan 21 '20 at 21:53
  • @Lior Then move them to where I said, and try again. See [accepted answer](https://stackoverflow.com/a/18093929/5221149) to question [What does “Could not find or load main class” mean?](https://stackoverflow.com/q/18093928/5221149), and look at reasons #2b, #2a, and #3. – Andreas Jan 21 '20 at 22:41
  • try to recompile. I would not suggest to to write `first = null;` as this is just its default value. – lue Jan 23 '20 at 20:09

0 Answers0