Treemap get抛出NullPointerException

时间:2015-08-03 07:42:54

标签: java

Follwing是我的java类TestEntry.java

private void initializemapTest()
    {
        eventMap = new TreeMap<String,String>();

        //Put some value into eventMap

        mapTest = new TreeMap<String, String>( new Comparator<String>()
        {
            public int compare( String key1, String key2 )
            {
               if( key1 == null )
                {
                    if( key2 == null )
                    {
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                }
                else
                {
                    if( key2 == null )
                    {
                        return -1;
                    }
                    else
                    {
                        return key1.compareTo( key2 );
                    }
                }
            }
        } );

        for( String s : eventMap.keySet() )
        {
            mapTest.put( eventMap.get( s ), s );   //Error at this line
        }

    }

根据我的知识,eventMap不允许空值,因此eventMap的键集没有任何空值,  如果eventMap中任何键的值为null,而我尝试将其放在mapTest中,则它不会抛出任何空指针异常,因为它各自的比较器允许空值

但为什么我得到这个例外

       java.lang.NullPointerException
        at java.util.TreeMap.cmp(TreeMap.java:1911)
        at java.util.TreeMap.get(TreeMap.java:1835)
        at kidiho.sa.client.reports.ReportEntry.initializemapTest(TestEntry.java:22)

3 个答案:

答案 0 :(得分:8)

它会抛出NullPointerException,因为在TreeMap中,api get()方法会故意抛出NullPointerException,如果它是null

final Entry<K,V> getEntry(Object key) {
        // Offload comparator-based version for sake of performance
        if (comparator != null)
            return getEntryUsingComparator(key);
        if (key == null)
            throw new NullPointerException();
        Comparable<? super K> k = (Comparable<? super K>) key;
        Entry<K,V> p = root;
        while (p != null) {
            int cmp = k.compareTo(p.key);
            if (cmp < 0)
                p = p.left;
            else if (cmp > 0)
                p = p.right;
            else
                return p;
        }
        return null;
    }

答案 1 :(得分:1)

从TreeMap:

    final Entry<K,V> getEntry(Object key) {
    // Offload comparator-based version for sake of performance
    if (comparator != null)
        return getEntryUsingComparator(key);
    if (key == null)
        throw new NullPointerException();
    Comparable<? super K> k = (Comparable<? super K>) key;
    Entry<K,V> p = root;
    while (p != null) {
        int cmp = k.compareTo(p.key);
        if (cmp < 0)
            p = p.left;
        else if (cmp > 0)
            p = p.right;
        else
            return p;
    }
    return null;
}

那就是:TreeMap不允许使用null键,所以你不能这样做:

tm.put(null, something)

然后,你做不到

tm.get(null)

根据TreeMap行为,这些操作实际上没有意义

答案 2 :(得分:1)

正如其他人所说,你不能使用null值作为TreeMap键,它会抛出NullPointerException

你没有从同一个地方得到NullPointerException可能是因为你的第一张地图有一个注册的比较器而第二张没有。