0

In the following code i get the nullpointer exception only in some cases and the JSON is the same every time. How to resolve this

Error

errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [POST] /school/sd/
Cannot set property 'school' on null object. Stacktrace follows:
Message: Cannot set property 'school' on null object

controller

def save() {

    if (!requestIsJson()) {
        respondNotAcceptable()
        return
    }


    println request.GSON
    def sInstance = new School(request.GSON)
    println "got here"

    if (sInstance.save(flush: true)) {
        respondCreated sInstance

    } else {

        respondUnprocessableEntity sInstance
    }


    def resp = RestClientHelper.createExpGroup(sInstance)

}
4

1 回答 1

0

为了从 JSON 创建 Grails 对象,很容易使用特殊的转换器。

import grails.converters.JSON
.......
class Controller {
  def doSomthing = {
      def myDomain = new MyDomain(JSON.parse(params.myDomain))
      //Save domain object
      myDomain.save(flush:true)
  }
}

Grails 转换器参考。

于 2013-10-22T10:05:14.460 回答