我正在使用带有 gwt 的谷歌应用引擎。当我通过查询问题访问投票时,我有两个类问题和投票,它给了我空指针异常。
我的课如下问题
@PersistenceCapable(identityType=IdentityType.APPLICATION,table="question")
public class Question implements Serializable {
private static final long serialVersionUID = 6355873282541087204L;
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String title;
@Persistent(dependent="true")
private Vote vote;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Vote getVote() {
return vote;
}
public void setVote(Vote vote) {
this.vote = vote;
}
}
投票
@PersistenceCapable(identityType=IdentityType.APPLICATION,table="vote")
public class Vote implements Serializable{
private static final long serialVersionUID = -1331159194642997258L;
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private int like;
@Persistent
private int unlike;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getLike() {
return like;
}
public void setLike(int like) {
this.like = like;
}
public int getUnlike() {
return unlike;
}
public void setUnlike(int unlike) {
this.unlike = unlike;
}
}
我在 serverImpl 类中的代码
@Override
public List<QuestionDTO> viewAllQuestion() {
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery(Question.class);
List<Question> list=null;
try{
list = (List<Question>) query.execute();
}finally{
pm.close();
}
List<QuestionDTO> questionDtoList = new ArrayList<QuestionDTO>();
for(Question question : list){
QuestionDTO questionDTO = new QuestionDTO();
questionDTO.setId(question.getId());
questionDTO.setTitle(question.getTitle());
VoteDTO voteDTO = new VoteDTO();
voteDTO.setId(question.getVote().getId());
voteDTO.setLike(question.getVote().getLike());
voteDTO.setUnlike(question.getVote().getLike());
questionDTO.setVoteDTO(voteDTO);
questionDtoList.add(questionDTO);
}
return questionDtoList;
}
错误是
Caused by: java.lang.NullPointerException
at com.wa.jdocascadedemo.server.impl.QuestionServiceImpl.viewAllQuestion(QuestionServiceImpl.java:86)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
... 22 more
我哪里错了?提前致谢