上記のデータ構造を呼び出すと、スタックとしてインスタンス化されたときに nullPointerException が返されるが、リストとしてインスタンス化されたときには返されないという奇妙な問題があります。
スタッククラスで実行すると、ユニットテストとプレイWebサーバーの実行の両方でnullPointerExceptionが発生します
これは機能します:
....
/*
* comment belongs to post, and if post is deleted,
* the deletion is relayed to all comments that post object
* owns
*/
@OneToMany (mappedBy="post",cascade=CascadeType.ALL)
public List<Comment> comments;
....
public Post(SuperUser author,String content,String title){
this.comments = new LinkedList<Comment>();
this.author = author;
this.content = content;
this.title = title;
this.postedAt = new Date();
}
...
public Post addComment(Comment newComment){
this.comments.add(newComment);
this.save();
return this;
}
これは動作しません:
....
/*
* comment belongs to post, and if post is deleted,
* the deletion is relayed to all comments that post object
* owns
*/
@OneToMany (mappedBy="post",cascade=CascadeType.ALL)
public Stack<Comment> comments;
....
public Post(SuperUser author,String content,String title){
this.comments = new Stack<Comment>();
this.author = author;
this.content = content;
this.title = title;
this.postedAt = new Date();
}
...
public Post addComment(Comment newComment){
this.comments.push(newComment); // ERROR
this.save();
return this;
}
List インターフェイスのラッパー インターフェイスを作成して、メソッドを指定してみました。
public E push(E elem);
しかし、それもうまくいきません。
これが何であるか考えていますか?