Grails 运行此代码时获取NullPointerException
我有一个Grails 运行此代码时获取NullPointerException,grails,groovy,nullpointerexception,Grails,Groovy,Nullpointerexception,我有一个User类,如下所示: package com.grailsinaction class User { String userId String password; Date dateCreated Profile profile static hasMany = [posts : Post] static constraints = { userId(size:3..20, unique:true)
User
类,如下所示:
package com.grailsinaction
class User {
String userId
String password;
Date dateCreated
Profile profile
static hasMany = [posts : Post]
static constraints = {
userId(size:3..20, unique:true)
password(size:6..8, validator : { passwd,user ->
passwd!=user.userId
})
dateCreated()
profile(nullable:true)
}
static mapping = {
profile lazy:false
}
}
package com.grailsinaction
class Post {
String content
Date dateCreated;
static constraints = {
content(blank:false)
}
static belongsTo = [user:User]
}
//other code goes here
void testAccessingPost() {
def user = new User(userId:'anto',password:'adsds').save()
user.addToPosts(new Post(content:"First"))
def foundUser = User.get(user.id)
def postname = foundUser.posts.collect { it.content }
assertEquals(['First'], postname.sort())
}
Post
像这样上课:
package com.grailsinaction
class User {
String userId
String password;
Date dateCreated
Profile profile
static hasMany = [posts : Post]
static constraints = {
userId(size:3..20, unique:true)
password(size:6..8, validator : { passwd,user ->
passwd!=user.userId
})
dateCreated()
profile(nullable:true)
}
static mapping = {
profile lazy:false
}
}
package com.grailsinaction
class Post {
String content
Date dateCreated;
static constraints = {
content(blank:false)
}
static belongsTo = [user:User]
}
//other code goes here
void testAccessingPost() {
def user = new User(userId:'anto',password:'adsds').save()
user.addToPosts(new Post(content:"First"))
def foundUser = User.get(user.id)
def postname = foundUser.posts.collect { it.content }
assertEquals(['First'], postname.sort())
}
我编写了一个集成测试,如下所示:
package com.grailsinaction
class User {
String userId
String password;
Date dateCreated
Profile profile
static hasMany = [posts : Post]
static constraints = {
userId(size:3..20, unique:true)
password(size:6..8, validator : { passwd,user ->
passwd!=user.userId
})
dateCreated()
profile(nullable:true)
}
static mapping = {
profile lazy:false
}
}
package com.grailsinaction
class Post {
String content
Date dateCreated;
static constraints = {
content(blank:false)
}
static belongsTo = [user:User]
}
//other code goes here
void testAccessingPost() {
def user = new User(userId:'anto',password:'adsds').save()
user.addToPosts(new Post(content:"First"))
def foundUser = User.get(user.id)
def postname = foundUser.posts.collect { it.content }
assertEquals(['First'], postname.sort())
}
我使用grails test app-integration
运行,然后得到如下错误:
Cannot invoke method addToPosts() on null object
java.lang.NullPointerException: Cannot invoke method addToPosts() on null object
at com.grailsinaction.PostIntegrationTests.testAccessingPost(PostIntegrationTests.groovy:23
哪里出错了?我猜
save()
方法返回null。请尝试以下方法:
def user = new User(userId:'anto',password:'adsds')
user.save() // Do you even need this?
user.addToPosts(new Post(content:"First"))
根据:
如果验证失败且实例未保存,则save方法返回null,如果成功,则返回实例本身
所以有可能你应该看看验证中出现了什么问题。。。例如,是否需要指定某些字段是可选的?(我不是Grails开发人员,只是想给你一些想法。)快速修复:你的密码必须在6到8个字符之间(检查你的约束字段) 一个愚蠢的想法,充其量就是为密码设置一个最大大小(最终它们应该被散列,并且与原始密码没有任何相似之处)
另一方面,我可以推荐Grails的权威指南吗?不是Grails开发人员,而是C#Developer;)这是有效的,我犯了一个错误,违反了验证:D谢谢你的回答@Ant的研究表明,了解多种技术是可能的