を実行したときに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)
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
を使用して実行し、その後エラーが発生しますこのように:
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#開発者;);)これが動作し、私は間違いを犯して検証に違反しました。 –
@Ant's it *は複数の技術を知ることができます –