我是Grails的新手,正在创建我的第一个应用程序,并且发现事情发生的地方有些麻烦,因此,我有一个对象“cicle”,其中包含许多“测量”,并且分别由测量 Controller 创建当我创建一个新的“度量”时,这是在另一台计算机上发生的,这是在同一台计算机上发生的,cicle的某些属性发生了变化,因此,我在cicle中执行了一个名为updateCalculations的方法,该方法需要新创建的度量的数据。
因此,在measurementController中,我试图使用循环中的测量值执行一些操作,但是我得到了空指针异常,我猜想未创建cicle,因此我将操作移至方法之后,将其插入Measurement对象中一个我认为更适合的地方这种方法需要开发,但是我在这里遇到了同样的问题,我什至认为创建cicle时购买了一些未初始化的度量,我不知道真正发生了什么,所以您能帮我吗?
这是我的代码:
测量方法
class Measurement{
String machine
int time1
int time2
int time3
int cicleValue
static belongsTo = [cicle: Cicle]
static constraints = {
machine blank:false
time1 nullable:false
time1 min:0
time2 nullable:false
time2 min:0
time3 nullable:false
time3 min:0
cicleValue nullable:false
cicleValue min:0
}
def afterInsert(){
this.cicle.updateCalculations()
}
public void updateCicleValue(){
double avgTime = (time1 + time2 + time3)/3
cicleValue = 30 * 24 * 60 * 60 / avgTime
}
}
Cicle.groovy
class Cicle {
String machine
int cicleValue
static hasMany = [measurements:Measurement]
static constraints = {
machine blank:false
cicleValue nullable:false
}
public void updateCalculations(){
int sumCicles = 0
measurements.each{ measurement ->
sumCicles += measurement.cicleValue
}
cicleValue = sumCicles / measurements.size()
this.save(failOnError: true)
}
}
MeasurementController.groovy
@Transactional(readOnly = true)
class MeasurementController {
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
def create() {
respond new Measurement(params)
}
@Transactional
def save(Measurement measurementInstance) {
if (measurementInstance == null) {
notFound()
return
}
measurementInstance.updateCicleValue()
Cicle cicle = Cicle.findByMachine(measurementInstance.machine)
if(cicle == null){
cicle = new Cicle(machine: measurementInstance.machine, cicleValue: measurementInstance.cicleValue)
cicle.save flush: true
}
measurementInstance.cicle = cicle
measurementInstance.validate()
if (measurementInstance.hasErrors()) {
respond measurementInstance.errors, view:'create'
return
}
measurementInstance.save flush:true
cicle.measurements << measurementInstance
cicle.save flush: true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'measurementInstance.label', default: 'Measurement'), measurementInstance.id])
redirect measurementInstance
}
'*' { respond measurementInstance, [status: CREATED] }
}
}
protected void notFound() {
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'measurementInstance.label', default: 'Measurement'), params.id])
redirect action: "index", method: "GET"
}
'*'{ render status: NOT_FOUND }
}
}
}
这是来自控制台的堆栈跟踪:
|Loading Grails 2.3.7
|Configuring classpath
.
|Environment set to development
.................................
|Packaging Grails application
...........
|Compiling 1 source files
...............................
|Running Grails application
|Server running. Browse to http://localhost:8080/mock-app
....Error
|
2014-04-24 21:51:34,719 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver - NullPointerException occurred when processing request: [POST] /mock-app/measurement/save - parameters:
time3: 20
time2: 20
time1: 20
machine: 2-12
create: Crear
Cannot invoke method size() on null object. Stacktrace follows:
Message: Cannot invoke method size() on null object
Line | Method
->> 23 | updateCalculations in com.rpc.mock.app.Cicle
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 26 | afterInsert in com.rpc.mock.app.Measurement
| 153 | call . . . . . . . in org.grails.datastore.gorm.support.EventTriggerCaller$MethodCaller
| 96 | call in org.grails.datastore.gorm.support.EventTriggerCaller
| 47 | onApplicationEvent in org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener
| 51 | $tt__save in com.rpc.mock.app.MeasurementController
| 200 | doFilter . . . . . in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 1145 | runWorker . . . . in java.util.concurrent.ThreadPoolExecutor
| 615 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 744 | run . . . . . . . in java.lang.Thread
它停在afterUpdate方法中,告诉测量其null
任何帮助都会很棒
谢谢
最佳答案
这是因为您忘记将measurementInstance
添加到cicle.measurements
。
尝试在此处保存cicle
实例时添加它:
if(cicle == null){
cicle = new Cicle(machine: measurementInstance.machine, cicleValue: measurementInstance.cicleValue)
cicle.addToMeasurements(measurementInstance) //add to collection
cicle.save flush: true
}