1

I was designing a generic linked list to create a linked list of Strings.
However I keep getting this error :

Exception in thread "main" java.lang.NoSuchMethodError: Node.<init>(Ljava/lang/Object;)V
at LinkedList.addNode(LinkedList.java:10)
at LinkedList.<init>(LinkedList.java:22)
at Trial.main(Trial.java:7)


From the stack trace , the error is generated at LinkedList's addNode() method. Im including the definition to this method as well as the definition of the Node class.
LinkedList addNode()

public void addNode(T n) {
        Node<T> temp = new Node<T>(n);
        if(start==null) {
            start = temp;
            current = start;
        } else {
            end.setNext(temp);
        }
        end =temp;
    }


Node.java

public class Node<T>{
private T n;
Node next;
Node(T n) {
    this.n = n;
    next = null;
}
public void setNext(Node nextNode) {
    next = nextNode;
}
public Node getNext() {
    return next;
}
public T getN() {
    return n;
}
@Override
public String toString() {
    if(n instanceof String)
        return n.toString();
    else {
        return T.toString();
    }
}

}



LinkedList.java

public class LinkedList<T>{
Node start;
Node end;
Node current;
private static final long serialVersionUID = 901L;
    LinkedList(T n) {
        addNode(n);
    }
    public void addNode(T n) {
        Node<T> temp = new Node<>(n);
        if(start==null) {
            start = temp;
            current = start;
        } else {
            end.setNext(temp);
        }
        end =temp;
    }

    LinkedList(T[] n) {
        for(T print : n)
        addNode(print);
    }
    public void addNode(T[] n) {
        if(n!=null) {
            for (T values : n) {
                addNode(values);
            }
        }
    }

    public void incC() {
        current = current.getNext();
    }
    public void insert(T n) {
        Node newNode = new Node(n);
            if(current==start){
                newNode.setNext(current);
                start = newNode;
            }else {
                Node tempstart = start;
                Node prevAdd=null;
                while(tempstart!=current){
                    prevAdd = tempstart;
                    tempstart = tempstart.getNext();
                }
                prevAdd.setNext(newNode);
                newNode.setNext(current);
            }
    }

    public void find(T x) {
        Node tempstart;
        tempstart = start;
        while (tempstart!=null) {
            if(tempstart.getN()==x) {
                System.out.println("Element found");
                tempstart = tempstart.getNext();
            } else {
                tempstart = tempstart.getNext();
            }
        }
    }
    public void delete(T x) {
        Node previous=null;
        Node tempstart = start;
        while(tempstart!=null) {
        if(tempstart.getN()==x) {
            if(previous ==null) {
                previous = tempstart;
                tempstart = tempstart.getNext();
                start = tempstart;
                previous.setNext(null);
                previous = null;
            } else {
                tempstart = tempstart.getNext();
                previous.setNext(tempstart);
            }
        }else {
            previous = tempstart;
            tempstart = tempstart.getNext();
        }
        }
    }
    @Override
    public String toString() {
        Node tempNode = start;
        String str = "Values: ";
        while (tempNode!=null) {
            str = str + " " + tempNode.toString();
            tempNode = tempNode.getNext();
        }
        return str;
    }
}

Trial.java

public class Trial {
public static void main(String[] args) {
    String[] para = {"Hollo","this","is","me"};
    LinkedList<String> L1;
    L1 = new LinkedList<String>(para);
    System.out.println(L1);
}
1
  • can you share your code in Trial.main(...) which is calling the LinkedList Commented May 30, 2012 at 7:00

5 Answers 5

4
return T.toString();

this doesn't work. T is a type variable and only available at compile time due to type erasure.

But apart from that, I can't see what's wrong, you need to poost more code from your LinkedList class.

Sign up to request clarification or add additional context in comments.

4 Comments

@Sean Well, If I was to create a linked list of Integers then I would require that won't I ? I changed the code a little after I posted this. It looks like this now ublic String toString() { if(n instanceof String) return n.toString(); else { return T.toString(n); } }
@FasihKhatib no, just write return n.toString() or null-safe: return String.valueOf(n)
Alright , I will edit the post to include the entire defn of LinkedList.java
T.toString() won't work. You should almost certainly just return n.toString() in all cases.
2

You should declare the start, end, and current fields in LinkedList< T > and the next field in Node< T > as type Node< T >, not Node. Don't use raw types anywhere in the code, because they translate into Node< Object >.

6 Comments

That solved it for me , Sir however , I have a question. Why isnt next in Node class dectared with <T> and only in LinkedList<>
@FasihKhatib, I'm sorry but I don't follow your question. Did you eliminate all the raw mentions of Node yet?
Yes, I replaced every instance of Node with Node<T> :) Uhm... do you have any simple exercises on this topic of Generic class ? I would like to practice a bit more 0:)
Primitives will be "auto-boxed". If you call some generic method f( T t ) with a primitive like 42, Java will infer the type Integer (not int) for the argument.
|
1

Your class Nodedoes not compile, so it is likely that you should first fix that issue before continuing:

return T.toString();

does not make sense. Probably that just writing this:

return n.toString();

is enough for now.

8 Comments

@FasihKhatib Then edit your question and post the complete code for LinkedList.
@FasihKhatib I just ran your code without problems. I only replaced your Node.toString() with public String toString() { return n.toString();} and I could run it without problems. Are you sure that you have properly recompiled your classes?
Yes , Sir. Im using JCrestor LE so all the classes associated with Trial.java (which has the main() method that creates linked list) so I dont have to manually do anyhting
I believe Node<T> temp = new Node<>(n); is the error. Could it be ?
@FasihKhatib The error you are getting is that the constructor Node with an Object argument does not exist (but the class Node is actually found on the JVM's classpath). Since your code actually contains that constructor, I would have suspected that your class had not been recompiled when you wrote the toString method. Maybe try a "Clean" on your project
|
1

In Node.java, in the method

@Override 
public String toString() { 
    if(n instanceof String) 
        return n.toString(); 
    else { 
        return T.toString(); 
    } 
} 

// the below statement thows compilation error. return T.toString();

8 Comments

@JackAss I even Tried return n.toString(); but it still gives that error. Probably something to do with Node.java's constructor
toString() is non static method generally. So if T is a class then T.toString() means that class must have a default toString() as static. but that is not true. hence compilation error. If you take any Class by default it extends Object class hence you get non-static toString(). Hence n.toString() works and T.toString() results in compilation failure
I removed that statement so only n.toString() is there now but still there is an error. could it be there is an error in how I defined Node.java class ?
public void addNode(T n) { Node<T> temp = new Node<>(n); if(start==null) { start = temp; current = start; } else { end.setNext(temp); } end =temp; } here Node<T> temp = new Node<>(n); is not a valid statement.
@JackAss So how do I modify that ? Node is a stand alone class that encapsulates the data element of the List and has a pointer to the next. Please , tell me why it is an error and how to solve it
|
1

Your Node Constructor is not public, so it will not work if you call it from a class in another package.

1 Comment

The constructor will work as long as all the source files are in the same directory :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.