Null pointer exception when persisting(create) using EntityHome
myjboss2020 May 19, 2011 3:54 AMhi, I'm a newbie to seam 2. I'm attempting to do pretty simple thing with Seam. Created a web application and manually create an entity and persist.
I have used Seam Generate Entities to reverse engineer my sample table ( Users ). Here is what I've got..( Ive added ID generator stuff )
@Entity @Table(name = "USERS", schema = "MYSCMA") public class Users implements java.io.Serializable { private static final long serialVersionUID = -4129911784324770432L; private Long UId; private String userId; private String firstName; private String lastName; private String active; private int failedAttempts; private Date dateCreated; private Date dateModified; private String password; public Users() { } public Users(Long UId, String userId) { this.UId = UId; this.userId = userId; } public Users(Long UId, String userId, String firstName, String lastName, String active, int failedAttempts, Date dateCreated, Date dateModified, String password) { this.UId = UId; this.userId = userId; this.firstName = firstName; this.lastName = lastName; this.active = active; this.failedAttempts = failedAttempts; this.dateCreated = dateCreated; this.dateModified = dateModified; this.password = password; } @Id @Column(name = "U_ID", unique = true, nullable = false, precision = 22, scale = 0) @NotNull @SequenceGenerator(name="IDSequence", sequenceName="USR_SEQ") @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="IDSequence") public Long getUId() { return this.UId; } ... ...
It also generated UsersHome.java and UsersList.java
Haven't changed anything in UsersHome.java..here is the generated code..
@Name("usersHome") public class UsersHome extends EntityHome<Users> { /** * */ private static final long serialVersionUID = -2888773603946593589L; public void setUsersUId(BigDecimal id) { setId(id); } public BigDecimal getUsersUId() { return (BigDecimal) getId(); } @Override protected Users createInstance() { Users users = new Users(); return users; } public void load() { if (isIdDefined()) { wire(); } } public void wire() { getInstance(); } public boolean isWired() { return true; } public Users getDefinedInstance() { return isIdDefined() ? getInstance() : null; } }
Now, I'v created an Seam Action and trying to create a new Users entity object and persist to the database.
Here is what I'm doing in Action method..
UsersHome usersHome = new UsersHome(); Users newUser = usersHome.createInstance(); newUser.setActive("Y"); newUser.setUserId("manualUser"); newUser.setPassword("password"); Timestamp currentTimeStamp = new Timestamp(System.currentTimeMillis()); newUser.setDateCreated(currentTimeStamp); newUser.setDateModified(currentTimeStamp); newUser.setFailedAttempts(0); newUser.setFirstName("firstName"); newUser.setLastName("lastName"); usersHome.setInstance(newUser); usersHome.persist();
Even newbie like me, feel that I'm missing something here..like either using a session or some a persistent manager of some sort..but I couldn't work out how or what to use it..so I just test it out..
I'm getting the following error..
java.lang.NullPointerException
at org.jboss.seam.framework.Controller.debug(Controller.java:197)
at org.jboss.seam.framework.Home.createdMessage(Home.java:90)
at org.jboss.seam.framework.EntityHome.persist(EntityHome.java:87)
at org.domain.usermanager.session.UserCreator.userCreator(UserCreator.java:38)
I suspect I'm not setting something..but, as I said, not sure what..
Could you please let me know how I can use session/persistent manager with seam/hibernate combination.
What does EntityHome object do..and what is the correct way of manually creating an entity object and persisting.
Any suggestions/pointers would be very helpful.
Cheers.