Results 1 to 3 of 3
  1. #1
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Rep Power

    Default Operator < cannot be applied to java.lang.Object, Object

    Hi,I am making a program with a linked list and nodes and i am trying to use the generic templates.
    This is an example of what i put to define my class 'public class LinkedList<Object>' and then put Object for the type throughout the code. So far it has worked for the Node class and now i am trying to get it to work on the LinkedList class.
    The problem i am having is i am comparing to Object items for a sorting method to put the linked list in order.

    Java Code:
    public void insertion(Object grad, Object y_int)
    boolean found = false;
    current = head_ptr;
    while (found == false && current != sentinel)
    if (numberOfNodes == 0)
    //std::cout << "in insertion numbernodes = 0" << endl;
    addToTail(grad, y_int);
    found = true;
    if (current.get_Data_x() == grad)
    if (current.get_Data_y() < y_int) --------- this is the line that has the problem.
    I get the error 'operator < cannot be applied to java.lang.Object, Object'.
    Can anyone tell me how i can fix this problem or know what it could be?


  2. #2
    susan is offline Member
    Join Date
    Jul 2007
    Rep Power


    Generics is a powerful tool in Java. But I'm wondering why you're creating a linked list with an Object as the generic.
    My suggestion is that you call the generic in the standard Java way and that is with an uppercase T

    Java Code:
    public class LinkedList<T>
      	public T doSomething(T c)
    		return c;	
    Or at the least, make it a Linked List of Nodes. But I digress.

    Let us look at your code. I'm unsure what is returned with the method invocation get_Data_y(), but I am going to assume it's of some Object type. If that's the case you can not use a mathmatical notation such as < > == != etc on it.

    Now I know you're asking, but == worked on the above. Well it sort of did. It worked in the sense that is it comparing the two pointers. But they aren't checking to see if the values are the same. This is why with Objects (including the class String) you have to use an equals method of some type.

    Unfortionatly since you are using the base class of Object you don't have the nifty method compareTo like other classes usually do. All you have use of is equals:

    Object (Java 2 Platform SE 5.0)

    Anyways, If this isn't the problem, then please post your Node Class so that we can see if we can debug the problem from there.

  3. #3
    bubtub24 is offline Member
    Join Date
    Nov 2010
    Rep Power


    I know this was posted in 2007 but I just ran into the same problem and this was the first to come up on Google search.

    The problem is in the Node class. You must declare the link object <T> generic.

    So Node<T> link;

    instead of Node link;

Similar Threads

  1. Replies: 0
    Last Post: 01-25-2008, 11:13 PM
  2. size of a java object
    By ravian in forum New To Java
    Replies: 0
    Last Post: 11-12-2007, 09:19 AM
  3. Error: cannot be applied to (java.lang.String)
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:33 AM
  4. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts