Reputation: 10335
I got this error when I tried to modify my Controller (generated by scaffolding). What I want to do, is to add current user that I capture from SpringSecurity Service.
Payment class has User property inside.
I tried to inject the user value from springSecurityService.getCurrentUser()
;
When I tried this, it will produce error : Property [user] of class [class kks.Payment] cannot be null
I tried to debug, it looks fine but still producing this error ....
This will work well if I put user.id inside the form as hidden value .. but I didn't want do this ...
How to solve this problem ?
Here is my code:
class PaymentController {
def springSecurityService;
@Transactional
def save(Payment PaymentInstance) {
println("1..... Payment");
PaymentInstance.user = springSecurityService.getCurrentUser();
if (PaymentInstance == null) {
notFound()
return
}
println("2..... Payment"+springSecurityService.getCurrentUser().class);
println(PaymentInstance.user.id); // still okay !!!
if (PaymentInstance.hasErrors()) {
println("3..... hasErrors?"); // entered here with Property [user] of class > [class kks.Payment] cannot be null
respond PaymentInstance.errors, view: 'create'
return
}
println("3..... Payment");
PaymentInstance.save flush: true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: >[message(code: 'Payment.label',
default: 'Payment'), PaymentInstance.id])
redirect PaymentInstance
}
'*' { respond PaymentInstance, [status: CREATED] }
}
}
}
Upvotes: 0
Views: 39
Reputation: 7619
After inserting value in the paymentInstance
you should validate the instance like:
paymentInstance.user = springSecurityService.getCurrentUser();
paymentInstance.validate()
and then check for error.
PS: Please follow convention: variable name should be started from lower case character.
Upvotes: 1