title: JPA Many to Many trouble
body: <p>I have been reading and watching tons of tutorials and just can't seem to get the many to many relationship to work.  I have built a new project to simplify the project down to one many to many relationship.  Please take a look at the code below and lend some suggestions as to why this fails.  Currently I get a null pointer at the *keywd.getMaterialRecordList().add(record); line.  If I comment this out then I get the same null pointer on the next .get....add();.  If it matters I have abandoned Derby and moved to an H2 database.</p>
  
  <pre><code>@Entity
  @Table(name = "MATERIAL_RECORD")
  @XmlRootElement
  @NamedQueries({
  @NamedQuery(name = "MaterialRecord.findAll", query = "SELECT m FROM MaterialRecord m")})
  public class MaterialRecord implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "ID")
  private Integer id;
  @Column(name = "SDS_NUMBER")
  private Integer sdsNumber;
  @Column(name = "PRODUCT_NAME")
  private String productName;
  
  @ManyToMany(mappedBy = "materialRecordList")
  private List&lt;Keywords&gt; keywordsList;
  </code></pre>
  
  <hr>
  
  <pre><code>@Entity
  @Table(name = "KEYWORDS")
  @XmlRootElement
  @NamedQueries({
  @NamedQuery(name = "Keywords.findAll", query = "SELECT k FROM Keywords k")})
  public class Keywords implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  @Column(name = "KEY_ID")
  private Integer keyId;
  @Column(name = "KEYWORD_NAME")
  private String keywordName;
  @JoinTable(name = "KEYWORD_LOOKUP", joinColumns = {
      @JoinColumn(name = "KEY_ID", referencedColumnName = "KEY_ID")}, inverseJoinColumns = {
      @JoinColumn(name = "SDS_NUMBER", referencedColumnName = "SDS_NUMBER")})
  @ManyToMany
  private List&lt;MaterialRecord&gt; materialRecordList;
  </code></pre>
  
  <hr>
  
  <pre><code>public class JpaTest {
  public static void main (String [] args){
     MaterialRecord record = new MaterialRecord();
     record.setProductName("oofbar");
  
     Keywords keywd = new Keywords();
     keywd.setKeywordName("testing");
  
     Keywords keywd2 = new Keywords();
     keywd2.setKeywordName("testing2");
  
     record.getKeywordsList().add(keywd);
     record.getKeywordsList().add(keywd2);
  
     keywd.getMaterialRecordList().add(record);
     keywd2.getMaterialRecordList().add(record);
  
     EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaApplication26PU");
  
     EntityManager em = emf.createEntityManager();
     EntityTransaction tx = em.getTransaction();
     tx.begin();
     em.persist(record);
     em.persist(keywd);
     em.persist(keywd2);
     tx.commit();
  </code></pre>