在谷歌应用引擎中执行查询时获取空指针异常

时间:2012-04-25 13:08:30

标签: google-app-engine google-cloud-datastore parent-child jdo

我正在使用谷歌应用引擎与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

我哪里错了? 提前谢谢

1 个答案:

答案 0 :(得分:0)

我弄错了。我还没有将defaultFetchGroup =“true”分配给Question类中的Vote类对象。 当从数据存储区检索到持久化对象时,defaultFetchGroup =“true”是必要的。