title: Grails - Cannot get property 'id' on null object
body: <p>here is i have some problem. </p>
  
  <p>I have 2 domain class and i want to pass some parameter from one to second domain class and save on second. </p>
  
  <p>Here is first domain class Car </p>
  
  <pre><code>package carrentco
  
  class Car {
  
      String brand
      String model
      String fuelType
      BigDecimal pricePerDay
  
      static constraints = {
          brand(inList:["AUDI", "BMW", "MERCEDES", "NISSAN", "HONDA", "FORD"])
          model()
          fuelType(inList:["FUEL", "DIESEL", "AUTOGAS"])
          pricePerDay(min:0.0, max:1000.0)
  
      }
  }
  </code></pre>
  
  <p>And here is second one Registration </p>
  
  <pre><code>package carrentco
  
  class Registration {
  
      String firstName
      String lastName
      Date dateOfBirth
      String phoneNumber
      String email
      Date startOfRentDate
      Date endOfRentDate
      Car rentedCar
  
      static constraints = {
          firstName(nullable: false)
          lastName(nullable: false)
          dateOfBirth()
          phoneNumber(phoneNumber: true, nullable: false)
          email(blank:false, email:true)
          startOfRentDate()
          endOfRentDate()
      }
  }
  </code></pre>
  
  <p>What i wanna do; I create some cars list and i added to cars/index.gsp some button to each car (RENT CAR) when user click to this button he go to registration/create.gsp here user add some details of himself like name surname etc. I want to get here Car id which one he select from car/index.gsp automatically and save this form. When admin will look to this registration record he will see to Which car user want to rent. </p>
  
  <p>here is my cars/index.gsp </p>
  
  <pre><code> &lt;g:each in="${carList}" var="car"&gt;
                  &lt;p&gt;${car.id} &lt;/p&gt;
                  &lt;g:link action="create" controller="registration" params="${[carId : car.id ]}"&gt;
                      Rent Car
                  &lt;/g:link&gt;
  
              &lt;/g:each&gt;
  </code></pre>
  
  <p>and here is my registration/create.gsp </p>
  
  <pre><code>&lt;g:form action="save"&gt;
      &lt;div&gt;&lt;p&gt;Car ID : ${car.id} &lt;/p&gt;&lt;/div&gt;
      &lt;fieldset class="form"&gt;
          &lt;f:all bean="registration"/&gt;
      &lt;/fieldset&gt;
      &lt;fieldset class="buttons"&gt;
          &lt;input type="hidden" name="carId" id="carId" value="${car?.id}"/&gt;
          &lt;input type="text" name="yourName" required&gt;
          &lt;g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" /&gt;
      &lt;/fieldset&gt;
  &lt;/g:form&gt;
  </code></pre>
  
  <p>when i write this code i get this error, </p>
  
  <pre><code>URI
  /registration/create
  Class
  java.lang.NullPointerException
  Message
  Request processing failed; nested exception is org.grails.gsp.GroovyPagesException: Error processing GroovyPageView: [views/registration/create.gsp:38] Error executing tag &lt;g:form&gt;: Error evaluating expression [car.id] on line [29]: Cannot get property 'id' on null object
  Caused by
  Cannot get property 'id' on null object
  </code></pre>
  
  <p>When i tried this one in registration/create.gsp </p>
  
  <pre><code> &lt;g:form action="save"&gt;
                  &lt;fieldset class="form"&gt;
                      &lt;f:all bean="registration"/&gt;
                  &lt;/fieldset&gt;
                  &lt;fieldset class="buttons"&gt;
                      &lt;g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" /&gt;
                  &lt;/fieldset&gt;
              &lt;/g:form&gt;
  </code></pre>
  
  <p>Everything is okey. I can save the registration form with car id, But here i need to select from list which car id. So i don't want this i want this was selected automatically because if i click to rent car button from car list example car id 2 why i need to select in registration form again car id it's not good. </p>
  
  <p>here is my registration controller. </p>
  
  <pre><code>package carrentco
  
  import static org.springframework.http.HttpStatus.*
  import grails.transaction.Transactional
  
  @Transactional(readOnly = true)
  class RegistrationController {
  
      static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
  
      def index(Integer max) {
          params.max = Math.min(max ?: 10, 100)
          respond Registration.list(params), model:[registrationCount: Registration.count()]
      }
  
      def show(Registration registration) {
          respond registration
      }
  
      def create() {
          respond new Registration(params)
          def selectedCar = Car.get(params.carId)
          [car: selectedCar]
      }
  
      @Transactional
      def save(Registration registration) {
  
          if (registration == null) {
              transactionStatus.setRollbackOnly()
              notFound()
              return
  
          }
  
          if (registration.hasErrors()) {
              transactionStatus.setRollbackOnly()
              respond registration.errors, view:'create'
              return
          }
  
          registration.save flush:true
  
          request.withFormat {
              form multipartForm {
                  flash.message = message(code: 'default.created.message', args: [message(code: 'registration.label', default: 'Registration'), registration.id])
                  redirect registration
              }
              '*' { respond registration, [status: CREATED] }
          }
      }
  
      def edit(Registration registration) {
          respond registration
      }
  
      @Transactional
      def update(Registration registration) {
          if (registration == null) {
              transactionStatus.setRollbackOnly()
              notFound()
              return
          }
  
          if (registration.hasErrors()) {
              transactionStatus.setRollbackOnly()
              respond registration.errors, view:'edit'
              return
          }
  
          registration.save flush:true
  
          request.withFormat {
              form multipartForm {
                  flash.message = message(code: 'default.updated.message', args: [message(code: 'registration.label', default: 'Registration'), registration.id])
                  redirect registration
              }
              '*'{ respond registration, [status: OK] }
          }
      }
  
      @Transactional
      def delete(Registration registration) {
  
          if (registration == null) {
              transactionStatus.setRollbackOnly()
              notFound()
              return
          }
  
          registration.delete flush:true
  
          request.withFormat {
              form multipartForm {
                  flash.message = message(code: 'default.deleted.message', args: [message(code: 'registration.label', default: 'Registration'), registration.id])
                  redirect action:"index", method:"GET"
              }
              '*'{ render status: NO_CONTENT }
          }
      }
  
      protected void notFound() {
          request.withFormat {
              form multipartForm {
                  flash.message = message(code: 'default.not.found.message', args: [message(code: 'registration.label', default: 'Registration'), params.id])
                  redirect action: "index", method: "GET"
              }
              '*'{ render status: NOT_FOUND }
          }
      }
  }
  </code></pre>
  
  <p>It's my university project so i need help and so much i can't understand what i need to do for solve this problem.</p>