我有一个奇怪的问题,当实例化为堆栈时,对所述数据结构的任何调用都会给出 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);
但这也行不通。
知道这可能是什么吗?