three objects : tracker , topic ,message
relations of tracker and topic is one-to-many
relations of topic and message is one-to-many
now i need add a topic to tracker ,and at the same time add a message to topic
Code:
tk =(Tracker)session.load(Tracker.class,trackerid);
Topic tp=new Topic();
tp.setTopicId(topicid);
tp.setTopicOwner(owner);
tp.setTitle(title);
tp.setTracker(tk);
tp.setState(-1);
tp.setCreatedBy("paul");
tp.setCreatedTime(new Date());
tp.setLastUpdate(new Date());
Message ms=new Message();
ms.setMessageId(new Integer(0));
ms.setCreatedTime(new Date());
ms.setCreatedBy("paul");
ms.setBody(message);
ms.setTopic(tp);
tp.getMessages().add(ms);
tk.getTopics().add(tp);
session.save(tp);
and run the code tp.getMessages().add(ms) throw a exception:java.lang.NullPointerException
at com.beaconsystem.catseye3.actions.SaveTopicAction.execute(SaveTopicAction.java:101)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:465)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1422)
how do i resolve it