I have the following code:
Binding binding = new Binding();
binding.setVariable("story", storyFactory.newStory());
GroovyShell shell = new GroovyShell(binding);
exp = getExpression();
System.out.println("Expression: " + exp);
Object result = (Object)shell.evaluate(exp);
That produces the following NullPointerException and console output:
Expression: story.getDescriptions().get(0).setText(story.getDescriptions().get(0).getText().replace("@", "at").replace("\"", "'").replace("%", "percent"));
java.lang.NullPointerException
at com.me.myapp.GroovyEvaluator._evaluate(GroovyEvaluator.java:56)
at com.me.myapp.testing.expressions.Driver.runEvaluator(Driver.java:131)
at com.me.myapp.testing.expressions.Driver.run(Driver.java:65)
at com.me.myapp.testing.expressions.Driver.main(Driver.java:31)
null
Why is result
NULL? It can't be a problem with the story
instance, as I have verified in other code that getDescriptions()
and getDescriptions().get(0)
both return non-null values. Also, getDescriptions().get(0)
does return a non-null, non-empty String value. Thanks in advance!
It returns the value of:
story.getDescriptions().get(0).setText
I assume setText
has a void
return type
Therefore you get null
Try changing your expression to:
story.descriptions.get( 0 ).with { d ->
d.text = d.text.replace("@", "at")
.replace( '"', "'")
.replace( "%", "percent" )
d
}
(or all on one line like):
story.descriptions.get( 0 ).with { d -> d.text = d.text.replace("@", "at").replace( '"', "'").replace( "%", "percent" ) ; d }