我有一个User
类是这样的:获取NullPointerException异常,当我运行这段代码
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
}
}
Post
类是这样的:
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
,然后我得到一个错误r像这样:
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
我哪里出错了?
不是Grails开发人员而是C#开发人员;);)这工作,我犯了一个错误,违反了验证:D感谢您的答案! –
@蚂蚁的它*是*可能知道多种技术 –