Null Pointer Exception for Ebean Insert Query
-
26-12-2019 - |
سؤال
I have E bean model of my table Countries and Indicators. The indicators model looks like this
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;
}
and My countries model Looks like this
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;
}
and I am trying to call the insert function
private static void procM007_SP003(ExcelInd excelRow, String indicator_code) {
// Insert
Indicators indObj = new Indicators();
indObj.country.countryId=542L;
indObj.save();
however, the indObj.country.countryId causes null pointer exception.
Any help is appreciated. thanks :)
المحلول
By default instance variables are set to null
and as you have not initialized country
variable of object Indicators
, so it will be null. And hence setting countryId
on null object will lead to NullPointerException
.
You need to initialize country attribute of Indicators object also:
Indicators indObj = new Indicators();
indObj.country = new Countries();
indObj.country.countryId = 542L;
indObj.save();
لا تنتمي إلى StackOverflow