문제

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top