views:

108

answers:

1

Hi,

I'm using JPA (with Hibernate) and Gilead in a GWT project. On the server side I have this method and I'm calling this method twice with the same "campaign". On the second call it throws a null pointer exception in line 4 "campaign.getTextAds()"

public List<WrapperTextAd> getTextAds(WrapperCampaign campaign) {
  campaign = em.merge(campaign);
  System.out.println("getting textads for "+campaign.getName());
  for(WrapperTextAd textad: campaign.getTextAds()) {
      //do nothing
  }
  return new ArrayList<WrapperTextAd>(campaign.getTextAds());
}

The code in WrapperCampaign Entity looks like this

@OneToMany(mappedBy="campaign")
  public Set<WrapperTextAd> getTextAds() {
    return this.textads;
}
+1  A: 

Since line 3 doesn't blow, campaign is not null. The for each loop will throw NPE if the collection to be iterated is null (as will trying to initialize a new ArrayList with a null collection parameter). You should guard against this:

if(campaign.getTextAds() != null) {
    for(WrapperTextAd textad: campaign.getTextAds()) {
        //do nothing
    }
}
crunchdog
Yap I know that. It's just .getTextAds() is not supposed to return NULL. At the very least it should return an empty Set. However there are "TextAds" for this "campaign" in the database and on the first call I do get them. But on every later call (beginning from the second) this property is always null until I restart my entityfactory
iNPUTmice