Ms01
Ms01

Reputation: 4702

Second map value is always null, even if it prints out 1

I have some code getting data and then selecting it in order. For this I use simple maps that I may later access with ease (I thought..).

I use the following code within a loop to insert maps to another map named "companies":

def x = [:]
x.put(it.category[i], it.amount[i])
companies.put(it.company, x)

And I can surely write the result out: [Microsoft:[Food:1], Apple:[Food:1]]

But then, when I am about to get the food value of each company it always is null. This is the code I use to get the values:

def val = companies.get(it.company).get(key.toString())
def val = companies[it.company][key] // doesn't make a difference

Val is always null. Can someone help and / or explain why I have this error. What am I doing wrong? I mean, I can clearly see the 1 when I print it out..

Upvotes: 1

Views: 419

Answers (2)

Ms01
Ms01

Reputation: 4702

The solution was simple to make the category as a string:

x.put(it.category[i].toString(), it.amount[i])

And after that little fix it all works as expected.

Upvotes: 0

tim_yates
tim_yates

Reputation: 171084

My guess is that it.category[i] and key are completely different types...

One thing you could try is:

x.put(it.category[i].toString(), it.amount[i])

and then

def val = companies[it.company][key.toString()] // doesn't make a difference

Upvotes: 1

Related Questions