Ebean插入查询的空指针异常

时间:2014-04-14 10:52:32

标签: 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(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;
             }

我试图调用插入函数

  private static void procM007_SP003(ExcelInd excelRow, String indicator_code) {

    // Insert
    Indicators indObj = new Indicators();
    indObj.country.countryId=542L;
            indObj.save();

但是, indObj.country.countryId 会导致空指针异常。

感谢任何帮助。谢谢:))

1 个答案:

答案 0 :(得分:0)

默认情况下,实例变量设置为null,并且您尚未初始化对象country的{​​{1}}变量,因此它将为null。因此,在null对象上设置Indicators将导致countryId

您还需要初始化Indicators对象的country属性:

NullPointerException