Reputation: 23
I want to post three different version of my codes, out of which one working but I cannot go with that approach.
Version1: not working
List<LinkedHashMap> listOfRecords
LinkedHashMap a = [product: 'p1', cluster: 'c1', salesorg: 's1']
LinkedHashMap b = [product: 'p2', cluster: 'c2', salesorg: 's2']
LinkedHashMap c = [product: 'p2', cluster: 'c3', salesorg: 's2']
listOfRecords.add(a)
listOfRecords.add(b)
listOfRecords.add(c)
println("listOfRecords : "+listOfRecords)
println("listOfRecords groupby : "+listOfRecords.groupBy { it.cluster })
Script failed on line: 6, with error: An error occurred while processing this page.
Version2 : working, but I don't want to assign LinkedHashMap to list during the declaration.
LinkedHashMap a = [product: 'p1', cluster: 'c1', salesorg: 's1']
LinkedHashMap b = [product: 'p2', cluster: 'c2', salesorg: 's2']
LinkedHashMap c = [product: 'p2', cluster: 'c3', salesorg: 's2']
List listOfRecords=[a]
//listOfRecords.add(a)
listOfRecords.add(b)
listOfRecords.add(c)
println("listOfRecords : "+listOfRecords)
println("listOfRecords groupby : "+listOfRecords.groupBy { it.cluster })
Version3: not working. Since it has a validation error, I tried with version1 and version2
//List<LinkedHashMap> listOfRecords
LinkedHashMap a = [product: 'p1', cluster: 'c1', salesorg: 's1']
LinkedHashMap b = [product: 'p2', cluster: 'c2', salesorg: 's2']
LinkedHashMap c = [product: 'p2', cluster: 'c3', salesorg: 's2']
List listOfRecords;
//List listOfRecords=[a]
listOfRecords.add(a)
listOfRecords.add(b)
listOfRecords.add(c)
println("listOfRecords : "+listOfRecords)
println("listOfRecords groupby : "+listOfRecords.groupBy { it.cluster })
A validation error was received from the Planning server. 'Error:The Groovy script failed to compile with internal error: Compile Error: [Static type checking] - No such property: cluster for class: java.lang.Object @ line 13, column 60. Rule SalesPLN.SALESREP.linkedhashmap'
Thanks in advance.
Upvotes: 0
Views: 2285
Reputation: 37033
Running your original script gives an Null Pointer Exception, because you never initialize listOfRecords
(E.g. this should fix it: def listOfRecords = []
). The code tries to add
to listOfRecords
, which triggers the NPE. Your second example does initialize your var and therefor works.
Why you get such a useless error message is beyond me though.
Upvotes: 1