我想要实现的是让用户检查一些复选框,之后我会对选中的项目执行一些操作。我在以下控制器代码中收到以下错误:
这是错误,它抱怨包含每个的行:
java.lang.NullPointerException: Cannot execute null+null
控制器:
def activateSubscriptions (SubscriptionRequest subscriptionRequestInstance) {
def temp
println ("Params : $params.subscriptionRequestInstanceCheckBox , Count: $subscriptionRequestInstance.count()")
params['subscriptionRequestInstanceCheckBox'].each {
temp =new Subscriptions(userId:subscriptionRequestInstance.userId,packageId:subscriptionRequestInstance.packageId,dasubscriptionRequestInstanceOfPackExp:subscriptionRequestInstance.dateCreated+30,balanceOfAds:Packages.get(subscriptionRequestInstance.packageId).numOfAds,
balanceOfMsgs:Packages.get(subscriptionRequestInstance.packageId).numOfMsgs,balanceOfHR:Packages.get(subscriptionRequestInstance.packageId).numOfHR,balanceOfPages:Packages.get(subscriptionRequestInstance.packageId).numOfPages).save(flush:true)
SubscriptionRequest.get(subscriptionRequestInstance.id).delete(flush:true)
}
}
这是GSP:
<g:each in="${subscriptionRequestInstanceList}" status="i" var="subscriptionRequestInstance">
<tr class="${(i % 2) == 0 ? 'even' : 'odd'}">
<td>
<g:checkBox checked="false" name='subscriptionRequestInstanceCheckBox' value="${subscriptionRequestInstance.id}"/>
<g:link action="show" id="${subscriptionRequestInstance.id}">${fieldValue(bean: subscriptionRequestInstance, field: "userId")}</g:link>
</td>
<td>${fieldValue(bean: subscriptionRequestInstance, field: "packageId")}</td>
<td><g:formatDate date="${subscriptionRequestInstance.dateCreated}" /></td>
</tr>
</g:each>
虽然println
的值为:
Params : [1, 2] , Count: 2
有什么想法吗?
答案 0 :(得分:0)
构造函数中的这个语句看起来是个问题:
dasubscriptionRequestInstanceOfPackExp:subscriptionRequestInstance.dateCreated+30
如果目标只是修复例外,请将其更改为:
dasubscriptionRequestInstanceOfPackExp: (subscriptionRequestInstance.dateCreated ? subscriptionRequestInstance.dateCreated + 30 : null)
这假设dasubscriptionRequestInstanceOfPackExp
字段可以为空,或者您可以保存失败。
不是一遍又一遍地使用Packages.get(subscriptionRequestInstance.packageId)
,为什么不将它存储在这样的变量中呢?从性能的角度来看,它在这一点上可能毫无意义,但它会清除构造函数的参数。
def activateSubscriptions (SubscriptionRequest subscriptionRequestInstance) {
def temp
println ("Params : $params.subscriptionRequestInstanceCheckBox , Count: $subscriptionRequestInstance.count()")
def package = Packages.get(subscriptionRequestInstance.packageId)
params.subscriptionRequestInstanceCheckBox.each {
temp = new Subscriptions(
userId: subscriptionRequestInstance.userId,
packageId: subscriptionRequestInstance.packageId,
dasubscriptionRequestInstanceOfPackExp: subscriptionRequestInstance.dateCreated + 30,
balanceOfAds: package.numOfAds,
balanceOfMsgs: package.numOfMsgs,
balanceOfHR: package.numOfHR,
balanceOfPages: package.numOfPages
).save(flush:true)
SubscriptionRequest.get(subscriptionRequestInstance.id).delete(flush:true)
}
}