user3435407
user3435407

Reputation: 1067

Constructor undefined, says Eclipse, but its defined (Java)

I don't get it, why is this constructor not defined. I check some other similar questions here, but there was always the problem, that they wanted to call the constructor without parameters, while the constructor had parameters. But I don't see this problem in my code. Could you please help me? Thank you!

I get the error message: The constructor Node(int) is undefined


The class with the main method:

package LLP;

public class LinkedList2Test {

public void main (String args[]){
    LinkedList2 test = new LinkedList2();

    test.add(13);
    test.add(10);
    test.add(21);
}
}

The LinkedList2 class

package LLP;

public class LinkedList2 {

Node head;
Node tail;

public void add(int data){

    **Node node = new Node(data);**// **THE PROBLEM is here**

    if (tail == null){
        tail = node;
        head = node;
    } else {
        tail.nextNode = node;
        tail=node;
    }
}
}

The Node class

package LLP;

public class Node {
int data;
Node nextNode;

public Node (int data){
    this.data = data;
}
}

As I see, in the main method I give in an integer, for example '13'. The add method receives this integer and calls it as 'data' And I would like to create the node with that 'data' Node's constructor needs just one integer, which would be 'data' so now 13 for example

Why does it not work, i dont get it...

Many Thanks

Upvotes: 3

Views: 32966

Answers (5)

Joey B
Joey B

Reputation: 1

In eclipse, the error “The constructor Node(int) is undefined” appeared after I right clicked on a folder and selected Build Path -> Use as Source Folder. To fix this issue, I followed the following steps:

  1. Rename the package containing the class(es) with the error
  2. Create a new folder using the old folder (package) name
  3. Create any preexisting subfolders under the folder just created
  4. Move the old class(es) from the renamed package to its rightful place in the new folder structure
  5. Once all the classes have been moved from the renamed package to the new folder structure, delete the package
  6. Perform a Project -> Clean

Upvotes: 0

Mae Warren
Mae Warren

Reputation: 21

This appears to be a glitch with Eclipse. For me it occurred following the renaming of the class being instantiated, and refactoring.

For me Ctrl-x Ctrl-v worked only temporarily.

What worked for me was closing the Editor tab of the class being instantiated. In fact closing all tabs in the Eclipse editor (and reopening those you need) seems to clear Eclipse's confusion.

Upvotes: 0

Igor Medvedyev
Igor Medvedyev

Reputation: 59

I had that problem a couple of times now with eclipse. What usually works is selecting a line where the error is and a sequence of

ctrl+x

ctrl+v

Upvotes: 5

Templar
Templar

Reputation: 1871

It should work. I guess it could be that you didn't save your Node class after you provided a constructor hence the error.

Also as @mypal125 you probably want static main method in your LinkedList2Test class. After changing that try to run (running also automatically saves all the changes) your program and see if there is still an error.

Upvotes: 2

morenoadan22
morenoadan22

Reputation: 234

The LinkedList2 class is missing the constructor.

Add this to your LinkedList2 class:

public LinkedList2(int data){ }

Upvotes: 0

Related Questions