Arthur Ronald
Arthur Ronald

Reputation: 33775

Error evaluating expression: Cannot invoke method getAt() on null object

I have a Question domain model designed as follows

class Question {

    List<Choice> choiceCollection;

    static hasMany = [choiceCollection:Choice]
    static mappping = {
        choiceCollection(joinTable:false)
    }

}

To fulfill my needs, /grails-app/views/question/create.gsp has been customized as you can see below

create.gsp

<g:each var="i" in="${(0..4)}">
    <div class="fieldcontain  required">
    <label for="description">
            Option ${i + 1}.
            <span class="required-indicator">*</span>
        </label>
        <g:textArea name="choiceCollection[${i}].description" cols="40" rows="5" maxlength="2000" value="${questionInstance?.choiceCollection[i]?.description}"/>
    </div>
</g:each>

When i try to access create view, i get the following error

Error evaluating expression [questionInstance?.choiceCollection[i]?.description]: Cannot invoke method getAt() on null object

Question: What should i do to run my application ?

Grails version: 2.1.1

Upvotes: 6

Views: 25219

Answers (3)

miles zarathustra
miles zarathustra

Reputation: 191

I observed this error when attempting to run grails (2.2.4) using Java 8. The cause was not immediately obvious. It's buried somewhere grails.util.BuildSettings.groovy

When I reverted to java 1.7, the message went away.

Upvotes: 9

Chris
Chris

Reputation: 8109

Instead of accessing by [] use getAt, then the ? Operator will work:

choiceCollection?.getAt(1)?.description

Upvotes: 9

rdmueller
rdmueller

Reputation: 11012

try to iterate only over the existing choices:

<g:each status="i" var="choice" in="${questionInstance.choiceCollection}">
    <div class="fieldcontain  required">
        <label for="description">
            Option ${i + 1}.
            <span class="required-indicator">*</span>
        </label>
        <g:textArea name="choiceCollection[${i}].description" cols="40" rows="5" maxlength="2000" value="${choice.description}"/>
    </div>
</g:each>

this should do the trick...

Upvotes: 0

Related Questions