Can't find symbol - method (generic method)

2019-09-04 20:23发布

问题:

I have two classes. One is a Phrase class,

import java.util.List;
import java.util.ArrayList;

@SuppressWarnings("unchecked")
public class Phrase
{
    List<String> papers = new ArrayList();
    String name = "";
    boolean multiple = false;

    public Phrase(String name, List list)
    {
        this.name = name;
        this.papers = list;
        if(list.size() > 1)
        {
             multiple = true;
        }
    }

    public Phrase(String name, String pName)
    {
        this.name = name;
        this.papers.add(pName);
        multiple = false;
    }

    public void addPaper(String paper)
    {
        papers.add(paper);
        multiple = true;
    }

    public String getPhrase()
    {
        return name;
    }

    public List<String> getPapers()
    {
         return papers;
    }
}

The Other is a KeyedLinkedList.

public class KeyedLinkedList<K,Phrase>
{
    private KeyNode first;
    private int size;

    private class KeyNode
    {
        K key;
        Phrase value;
        KeyNode previous;
        KeyNode next;

        public KeyNode(K key, Phrase value, KeyNode previous, KeyNode next)
        {
            this.key = key;
            this.value = value;
            this.previous = previous;
            this.next = next;
        }
    }

    public int size()
    {
        return size;
    }

    public boolean put(K key, Phrase val)
    {
        if(isEmpty())
        {
            first = new KeyNode(key, val, null, null);
            first.next = first;
            first.previous = first;
            size++;
            return true;
        }
        KeyNode temp = first;
        if(temp.key.equals(key))
        {

                //****ERROR LOCATION****//
            temp.value.addPaper(val.getPapers().get(0));
                //****ERROR LOCATION****//

            if(temp.value.getPapers().size() < 3)
                return false;
            return true;
        }
        temp = temp.next;
        while(temp != first)
        {
            if(temp.key.equals(key))
            {
                temp.value.addPaper(val.getPapers().get(0));
                if(temp.value.getPapers().size() < 3)
                    return false;
                return true;
            }
            temp = temp.next;
        }
        temp.previous.next = new KeyNode(key, val, temp.previous.next, first);
        first.previous = temp.previous.next;
        size++;
        return true;
    }
}

When I compile this I get the error: "Can't find symbol - method getPapers()"

I obviously have the getPapers() method in my Phrase class and val in the parameters is a Phrase object. I am wondering what I need to do to fix this problem. The error occurs half way through the put method.

回答1:

public class KeyedLinkedList<K,Phrase>
//                             ^^^^^^

Here, you're declaring a type variable which has the same name as the Phrase class and shadows it. Any variables declared with the type Phrase refer to this type variable instead of the Phrase class.

Since I have no idea what your intent was, I can't really advise anything except to remove that.

public class KeyedLinkedList<K>

BTW, don't do this:

List<String> papers = new ArrayList();
//                        ^^^^^^^^^

It's called a raw type, it's bad, and there's no reason to use it. Do new ArrayList<String> instead.