当字段为非null时,JavaBeans抛出NullPointer

时间:2014-01-09 16:58:24

标签: java database nullpointerexception javabeans

我的bean模型出了问题,我根本无法弄清楚为什么它会抛出一个nullpointer。

这是我的MessageModel:

@Entity
@Table(name = "privateMessages")
public class MessageModel {

@Id
@Column(unique = true, updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(unique = false, updatable = false)
private String userFrom;

@Column(unique = false, updatable = false)
private String userTo;

@Column(unique = false, updatable = false)
private String Message;

@Column(unique = false, updatable = true)
private Boolean isRead;

@Column(unique = false, updatable = true)
private Boolean isDeleted;

@Column(unique = false, updatable = true)
private String insertDate;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getUserFrom() {
    return userFrom;
}

public void setUserFrom(String userFrom) {
    this.userFrom = userFrom;
}

public String getUserTo() {
    return userTo;
}

public void setUserTo(String userTo) {
    this.userTo = userTo;
}

public String getMessage() {
    return Message;
}

public void setMessage(String message) {
    Message = message;
}

public Boolean getRead() {
    return isRead;
}

public void setRead(Boolean read) {
    isRead = read;
}

public Boolean getDeleted() {
    return isDeleted;
}

public void setDeleted(Boolean deleted) {
    isDeleted = deleted;
}

public String getDate() {
    return insertDate;
}

public void setDate(String date) {
    insertDate = date;
}

}

以下是我用来设置变量的方法:

    public void createUserNote(String msg, String playername, String playerto) {
    MessageModel model = new MessageModel();

    model.setRead(false);
    model.setUserFrom(playername);
    model.setUserTo(playerto);
    model.setDeleted(false);
    model.setMessage(msg);

    SimpleDateFormat format = new SimpleDateFormat("d M hh:mm");
    String date = format.format(new Date());
    model.setDate(date);

    plugin.getDatabase().save(model);
}

也许你可以帮助我! 提前谢谢!

2 个答案:

答案 0 :(得分:2)

我怀疑你的问题是它正在寻找一个名为'getInsertDate()'的方法,但你已经将方法重命名为getDate(),因此它不知道如何调用它。尝试将getter和setter更改回默认命名,然后重试。

NullPointer可能与获取执行方法失败有关,而不是执行方法失败。

答案 1 :(得分:1)

insertDate成员重命名为date,或将get/setDate重命名为get/setInsertDate

似乎有些库正在使用反射来尝试找到字段insertDate的getter / setter。由于setter与字段名称不匹配(因此不遵守JavaBeans约定),因此失败时会出现特别无用的错误消息。