0

I'm pretty new to groovy so bear with me but I've got a bunch of similar code that I'm trying to feed into an array. It's pretty straightforward I have my array declaration at the top and then declaring variables similar to each other below. I only included three but in reality I have about 10. Except this isn't working and I have no ideas why? If anyone has any ideas it is greatly appreciated.

def properties = resource.adaptTo(ValueMap.class) ?: [] 
headerText = properties["headerText"] ?: ""
bodyText = properties["bodyText"] ?: "" 
footerText = properties["footerText"] ?: ""

Error below:

Caused by: groovy.lang.MissingPropertyException: No such property: headerText for class
6
  • 1
    It's to include what and how things aren't working. Are you sure you have an array declaration? Java arrays are indexed by numbers, not strings. Are you instead trying to make an empty map, [:]? What's a ValueMap? What's resource.adaptTo? Does it include mapped property access? Does the property exist? etc. Commented Jun 10, 2013 at 15:17
  • I assume you want a map ([:], as Dave said). Other languages (PHP, I'm looking at you) call that an "array" even 'though it's an associative map. And "array" in Java/Groovy is more like a list. Commented Jun 10, 2013 at 15:22
  • If I'm understanding this correctly then my code would be something similar to: def properties = resource.adaptTo(ValueMap.class) ?: [:] ?? Commented Jun 10, 2013 at 15:27
  • What happened when you tried it? Commented Jun 10, 2013 at 15:31
  • Still getting the same error. Commented Jun 10, 2013 at 15:40

1 Answer 1

1

I'm probably missing something, it seems to easy--but:

def properties = resource.adaptTo(ValueMap.class) ?: [:] 
def headerText = properties["headerText"] ?: ""
def bodyText = properties["bodyText"] ?: "" 
def footerText = properties["footerText"] ?: ""

seems like it should work.

If you are running it as a script (not inside a class) remove ALL the defs--they are required in classes but will break scripts--but if you were running it as a script I would have expected to see it say that "properties" was not defined, not "headerText" so I assume it's part of a class.

Sign up to request clarification or add additional context in comments.

4 Comments

[] is a list, so that part, at least, will never work. But IMO the rest is also incorrect; it looks like there just isn't a "headerText" exposed as a property. It's the same error you'd get if you try to access a class property that doesn't exist, e.g., class Foo { def fname }; f = new Foo(); f["wat"]
wow I feel like an idiot it was because I didn't declare def thanks for the help!
Then I take it back :) Except for the list part.
Changed the list to a map default--should fix that. Thanks Dave.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.