0

I want to access any index of a HashMap to retrieve an Id through this code:

th:text="'Network Id: ' + ${poolHashrates[0].key.networkHashrate.id}"

poolHashrates is a HashMap,
networkHashrate is a separate variable in key variable

Basically, all the networkHashrates in key have the same Id, so I could actually access any element, no matter what the index is.

I've also tried:

th:text="'Network Id: ' + ${poolHashrates.get(key).networkHashrate.id}"
th:text="'Network Id: ' + ${poolHashrates['key'].networkHashrate.id}"

None of these works. I keep getting

Exception evaluating SpringEL expression: "poolHashrates['key'].networkHashrate.id"

or

SpelEvaluationException: EL1007E: Property or field 'networkHashrate' cannot be found on null

I was able to print the Id in Intellij console, so it apparently exsists.

Maciaz
  • 1,084
  • 1
  • 16
  • 31

1 Answers1

1

So you want to get an element out of the HashMap without knowing any of the keys? I think that's kind of silly... but it is possible. Here is one such way:

<span th:text="${poolHashrates.get(poolHashrates.keySet().toArray()[0])}" />

That will get you an element of the poolHashrates HashMap. I'm still unclear why you keep talking about key, but never really define what you mean. Is key really a property (with a getKey() and setKey()) on whatever you are storing in your HashMap? If so, then your final expression would look like this:

<span th:text="${poolHashrates.get(poolHashrates.keySet().toArray()[0]).key.networkHashrate.id}" />

or maybe

<span th:text="${poolHashrates.get(poolHashrates.keySet().toArray()[0]).networkHashrate.id}" />

(Are you confusing .key with a variable you get when you iterate over a HashMap with th:each in Thymeleaf?)

Metroids
  • 18,999
  • 4
  • 41
  • 52