Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Uncompilable source code - Erroneous tree type: <any>

800965Jul 15 2011 — edited Jul 15 2011
Hi, I have an example class SlowMap taken from a book, unfortunatelly it doesn't compile and throws this exception:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
	at chapter17.SlowMap.entrySet(SlowMap.java:47)
	at java.util.AbstractMap.toString(AbstractMap.java:478)
	at java.lang.String.valueOf(String.java:2826)
	at java.io.PrintStream.println(PrintStream.java:771)
	at chapter17.SlowMap.main(SlowMap.java:54)
Java Result: 1
This is the code:
import java.util.*;

public class SlowMap<K, V> extends AbstractMap<K, V> {
    private List<K> keys = new ArrayList<K>();
    private List<V> values = new ArrayList<V>();
    
    @Override
    public V put(K key, V value) {
        V oldValue = get(key);
        if (!keys.contains(key)) {
            keys.add(key);
            values.add(value);
        } else
            values.set(keys.indexOf(key), value);
        return oldValue;
    }
    
    @Override
    public V get(Object key) {
        if (!keys.contains(key))
            return null;
        return values.get(keys.indexOf(key));
    }
    
    @Override
    public Set<Map.Entry<K, V>> entrySet() {
        Set<Map.Entry<K, V>> set = new HashSet<Map.Entry<K, V>>();
        Iterator<K> ki = keys.iterator();
        Iterator<V> vi = values.iterator();
        while (ki.hasNext())
            // this line throws exception
            set.add(new Map.Entry<K, V>(ki.next(), vi.next()));
        return set;
    }
    
    public static void main(String[] args) {
        SlowMap<String, String> m = new SlowMap<String, String>();
        m.putAll(Countries.capitals(6));
        System.out.println(m);
        System.out.println(m.get("BUĊGARIA"));
        System.out.println(m.entrySet());
    }
}
I don't understand the exception message, so I have no clue how to fix the code. Can anyone tell me what is wrong with this code?

Thanks,
PR.
This post has been answered by walterln on Jul 15 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 12 2011
Added on Jul 15 2011
5 comments
16,685 views