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

18,523

Solution 1

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

choiceCollection?.getAt(1)?.description

Solution 2

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.

Share:
18,523
Arthur Ronald
Author by

Arthur Ronald

IT analyst Feel free to contact me at arthurseveral [at] gmail [dot] com

Updated on June 17, 2022

Comments

  • Arthur Ronald
    Arthur Ronald almost 2 years

    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

  • Arthur Ronald
    Arthur Ronald over 11 years
    Nice one (+1). Just for curiosity: why getAt method whether List API does not define one ???
  • Chris
    Chris over 11 years
    Thx. Regarding your question:There is one in the java.util.List of groovy: groovy.codehaus.org/groovy-jdk/java/util/List.html
  • Korobko Alex
    Korobko Alex about 9 years
    Can I just use get instead of getAt?
  • Chris
    Chris about 9 years
    Unfortunately not always, but possibly in this specific case