I have a object called Setor, which is already saved. Now, I want to add an object Quadra to Setor, and many objects Lote to Quadra. Neither Quadra or Lote are saved. The following code shows the way I'm trying to do it (I get a null pointer exception). It may give you an idea of what i want to do.
Code:
Setor set =
(Setor) session.load(Setor.class, new Long(paf.getId()));
int k = Integer.parseInt(paf.getQuantLotes());
Quadra quadra = new Quadra();
quadra.setNome(paf.getNome());
quadra.setComentario(paf.getComentario());
quadra.setSetor(set);
set.getQuadras().add(quadra);
Long newId = (Long)session.save(quadra);
Quadra quad =
(Quadra) session.load(Quadra.class, newId);
for (int i = 0; i <= k; i++){
Lote lote = new Lote();
lote.setNome(new Integer(i+1).toString());
lote.setQuadra(quad);
lote.setSetor(set);
lote.setComentario("");
lote.setLivre(true);
quad.getLotes().add(lote);
session.save(lote);
}
session.flush();
session.close();
The problem is: I'm getting a NullPointerException at line "quad.getLotes().add(lote);" (I can say that beause I'm logging it - I cut the log4j code for readable reasons).
Would anyone tell me what I'm doing wrong?
Thanks a lot