#47 HashMap constructor bug

open
nobody
None
5
2009-08-21
2009-08-21
No

the following throws the browser error Error: uncaught exception: MethodNotFoundException:Method java.util.HashMap.putAll(java.util.HashMap) is not found!:

HashMap hm = new HashMap();
hm = new HashMap(new HashMap());

if I change j2s.java.core HashMap.java involucred constructor to the following, this works OK:

public HashMap(Map<? extends K, ? extends V> map) {
this(map.size() < 6 ? 11 : map.size() * 2);
((AbstractMap)this).putAll(map);//cancerbero_sgx. before was: super.putAll(map);
}

I attached the HashMap.java file with this fix. I don't have any clue about why the code throws the exception... :(

Discussion

  • Sebastián Gurin

    correction

     
  • Sebastián Gurin

    a little further investigation. The bug is not only in the constructor but in all calls of super.putAll() in HashMap.java. For example, the following will fail the same as before:

    public static HashMap buildHM() {
    HashMap hm = new HashMap();
    hm.put("a", "aaaa");
    return hm;
    }
    public static void main(String[] args) {
    HashMap hm = new HashMap();
    HashMap hm2 = buildHM();
    hm.putAll(hm2 );
    }

    but this will work OK :

    HashMap hm = new HashMap();
    hm.putAll(new HashMap());

    ¿¿??

    pd . unfortunately I can't fine an easy fix for this... :(

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks