I have three classes one which is class User contains information about users name, last area code etc.... and it must implement Comparable but receiving generic parameters. Another one is a custom doubleLinkedList which basically has a link to the right and one to the left and contains a generic variable as the info, this is all it must have the method compareTo() cannot be in here since I need this class to be reusable such as instead of receiving User objects it can use receive other types of data such as Integers and Doubles etcc... The LinkedList has various methods one which is insert() which has to insert an object of any kind but in an ordered manned using compareTo() from the Java API but in this case I have to call the compareTo() method from the User class so that once it receives the two generic variables it compares the name and not the whole object. The thing is that this method is implemented in the User class. My problem is that when I send the generic variable with the method it doesn't recognize as it being a User object so I can't get the method getName() from that object to compare the two strings. Here's the code any help would be greatly appreciate, I changed some of the things to English so it's better understandable.

public class Usuario <T> implements Comparable <T>
    private String nombre;
    private String apellido;
    private char tipo;
    private short lada;
    private int telefono;
    public Usuario(String nom, String apel, char type, short area, int tel)
        nombre = nom;
        apellido = apel;
        tipo = type;
        lada = area;
        telefono = tel;
    public String getName()
        return nombre;
    public int compareTo(Usuario obj)
        return nombre.compareTo(obj.getName());


public class ListaDoble <T>
    class Nodo
        public T info;
        public Nodo enlace;
        public Nodo anterior;
        Nodo(T m)
            info = m;
            enlace = null;
            anterior = null;
    Nodo start;
    Nodo cola;
    int tamanno;
    public ListaDoble()
        start = null;
        cola = null;
        tamanno = 0;
    public void insert(T obj)
        Nodo temp = start;
        Nodo n = new Nodo(obj);
        if(temp == null)
            inicio = n;
//The problem is here I call the method compareTo() I need to call the method that will be stored in temp.info so that it can go to the method get the String and compare it to the other String
            if(temp.info.compareTo(n.info) >= 0)
    public Object search(Object)
public class methods
        ListaDoble <Usuario> Nodos = new <Usuario> ListaDoble();
        //The variables j, k, l, m, n have the adecuate variables as creating an object os type Usuario and I send the method to insert it
        Usuario nuevo = new Usuario(j, k, l, m, n);

Edited by adrianniebla: n/a

8 Years
Discussion Span
Last Post by JamesCherrill

Exactly what error message do you get on exactly which line?

Also: you carefully test for temp == null just before attempting temp.info - null pointer perhaps?

Edited by JamesCherrill: n/a


I have no error but I dunno how to make the method add so that the generic variable obj calls method compareTo() from the User class, so that when I insert the variable it compares the names and organizes it like that.


I haven't spent a lot of time checking your code, but at first sight it looks OK to me. Try it! Remeber that generics are not generic at run time - the compiler and runtime will know what to do!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.