Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No signature of method: java.util.ArrayList.getAt() is applicable for argument types: (HashMap) values: [[:]]

I am using writing groovy code to create list of map. but when I am trying to add map in the list it giving me the exception

groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.getAt() is applicable for argument types: (HashMap) values: [[:]]
Possible solutions: getAt(int), getAt(groovy.lang.Range), getAt(groovy.lang.EmptyRange), getAt(int), getAt(java.lang.String), getAt(java.util.Collection)
        at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
        at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
        at 
        at sun.reflect.GeneratedMethodAccessor146.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
        at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1037)
        at groovy.lang.Closure.call(Closure.java:415)
        at groovy.lang.Closure.call(Closure.java:431)
        at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2187)
        at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2172)
        at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2213)
        at org.codehaus.groovy.runtime.dgm$166.invoke(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
        at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
        at ]
        at Current(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:190)
        at             at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)

So I have changed my code java style still I am getting same exception

Below is my code

List<Map<String,String>> data = new ArrayList()
                Map<String,String> map = new HashMap()
                datas.each{adv ->
                    if(adv.data.equalsIgnoreCase("myData)){
                        map.put("id",adv.id)
                        map.put("url", adv.url)
                }
                }
                data.add(map)//getting exception at this point  
like image 795
LowCool Avatar asked Nov 08 '22 13:11

LowCool


1 Answers

Following code should work for you,

    List data = []
    Map map = [:]
     datas.each{adv ->
                        map = [:] //emptying map 
                        if(adv.data.equalsIgnoreCase("myData)){
                            map["id"] = adv.id
                            map["url"] = adv.url
                        }
                 data << map // inserting map into list
                }

     println data.dump() // printing list of maps
like image 127
Nitin Dhomse Avatar answered Nov 15 '22 05:11

Nitin Dhomse