0

在我的应用程序中,我有几个持久化模型,在一种情况下,我在尝试使用相应的 getter 获取数据时遇到异常。但是数据库中有数据,我不知道为什么 getter 什么都不返回。答题类:

@DatabaseTable
@JsonRootName(value = "answer")
public class Answer {
    public Answer() {}        

    @DatabaseField(generatedId = true, columnName = "_id")
    @JsonIgnore
    private Integer id;

    @DatabaseField
    @JsonProperty(value = "id")
    private Integer answer_id;

    @DatabaseField(dataType = DataType.LONG_STRING)
    private String text;
    // some other fields and setters/getters
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

问题类:

@DatabaseTable
public class Question extends BaseDaoEnabled implements Serializable {

    private static final long serialVersionUID = -7587892134579897L;

    public Question() {
    }
    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    private Answer answer;
}

任务类:

@DatabaseTable(tableName = "quests")
@JsonRootName(value = "quest")
public class Quest {

@DatabaseField(allowGeneratedIdInsert = true, generatedId = true, columnName = "_id")
private Integer id;

@DatabaseField(dataType = DataType.DATE_STRING)
Date created_at;

@DatabaseField(foreign = true)
private Survey survey;

@ForeignCollectionField(eager = false, orderColumnName = "question_id")
public Collection<Question> questions;

答案创建:

  answer.setText(input.getText().toString());
  try {
      if (answer.getId() == null) {
          DBHelperFactory.GetHelper().getAnswersDao().create(answer);
          question.setAnswer(answer);
          DBHelperFactory.GetHelper().getQuestionsDao().update(question);
      } else  {
          DBHelperFactory.GetHelper().getAnswersDao().update(answer);
      }
      Toast.makeText(this.getActivity(), "Answer saved", 5).show();
      listener.nextQuestion();
  } catch (SQLException e) {
      Log.e(LOG_KEY, "Create failed", e);
  }

以及失败的代码,(任务被传递给在后台运行的函数)

for (Question question : quest.questions) {
    if (question.getAnswer() != null) {
         Log.d(LOG_KEY, question.getId().toString());
         Log.d(LOG_KEY, question.getAnswer().getId().toString());
         Log.d(LOG_KEY, question.getAnswer().getText());
         // NullPointerException for getText().
    }
}

getAnswer()确实返回答案。数据库中的数据保持不变。但是getText()失败了。谁能帮我找出我错在哪里?

4

1 回答 1

0

关键是它的@DatabaseField(dataType = DataType.LONG_STRING)工作方式很奇怪,我无法让它工作。切换回后VARCHAR- 问题消失了。

于 2013-01-15T15:28:36.327 回答