Java 插入查询的空指针异常
我有我的表格国家和指标的E bean模型。 指标模型如下所示Java 插入查询的空指针异常,java,playframework,nullpointerexception,ebean,Java,Playframework,Nullpointerexception,Ebean,我有我的表格国家和指标的E bean模型。 指标模型如下所示 public class Indicators extends Model { private static final long serialVersionUID = 1L; @Id public Long id; @OneToMany(mappedBy = "indicator") public HashSet<Indicators> transactions; @ManyToOne @JoinColumn(n
public class Indicators extends Model {
private static final long serialVersionUID = 1L;
@Id
public Long id;
@OneToMany(mappedBy = "indicator")
public HashSet<Indicators> transactions;
@ManyToOne
@JoinColumn(name = "countryid")
public Countries country;
}
public class Countries extends Model {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "COUNTRYID")
public Long countryId;
@OneToMany(mappedBy = "country")
public HashSet<Countries> indicators;
}
但是,indObj.country.countryId会导致空指针异常
感谢您的帮助。谢谢:默认情况下,实例变量设置为null,并且由于您尚未初始化对象指示符的国家/地区变量,因此它将为null。因此,在null对象上设置countryId将导致NullPointerException 您还需要初始化Indicators对象的country属性:
Indicators indObj = new Indicators();
indObj.country = new Countries();
indObj.country.countryId = 542L;
indObj.save();
Indicators indObj = new Indicators();
indObj.country = new Countries();
indObj.country.countryId = 542L;
indObj.save();