Play框架“发生NullPointerException:null”

时间:2013-11-21 03:57:35

标签: java playframework playframework-1.x

我收到此错误,我理解此错误但我不明白为什么我收到此错误。我的模型Course有许多SessionSession属于1 Course。当我尝试将会话添加到课程的会话列表时,我得到了这个。这是我的代码

我的控制器方法:

public static void session(Course course, Integer sessionCapacity){
    Session session = new Session(course, sessionCapacity);
    course.save();
    session.save();
    if(session != null){
        course.sessions.add(session);
        course.save();
    }
    index();
}

我对表单的看法:

     #{form @Courses.session(), id:'session'}
            <input type="hidden" name="course" value = ${course} />
            <input type="text" name="sessionCapacity" />

           <input type="submit" value="Add a session" /> 
     #{/form}

我的会话模式:

@Entity
public class Session extends Model {
public Integer capacity;

@ManyToOne
Course course;

public Session(Course course, Integer capacity) {
    // TODO Auto-generated constructor stub
    this.course = course;
    this.capacity = capacity;
}
}

我的课程模型有这种关系:

 @OneToMany
public List<Session> sessions;

编辑1 :::: 所以我找到了这个link,根据它,我应该初始化List,所以我将课程模型更新为public List<Session> sessions = new ArrayList<Session>();,但它仍然不起作用。 :(

编辑2 :::

这是我得到的堆栈

17:05:56,680 DEBUG ~ select course0_.id as id4_1_, course0_.capacity as capacity4_1_,  course0_.courseCode as courseCode4_1_, course0_.courseDescription as courseDe4_4_1_, course0_.courseName as courseName4_1_, course0_.department_id as department6_4_1_, department1_.id as id0_0_, department1_.DepartmentCode as Departme2_0_0_, department1_.DepartmentName as Departme3_0_0_   from Course course0_ left outer join Department department1_ on course0_.department_id=department1_.id  where course0_.id=?
17:06:03,456 ERROR ~ 

@6gbd9kpep
Internal Server Error (500) for request POST /courses/session

Execution exception (In /app/models/Course.java around line 37)
NullPointerException occured : null

play.exceptions.JavaExecutionException
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException
at models.Course.AddSession(Course.java:37)
at controllers.Courses.session(Courses.java:36)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
... 1 more

和第37行是this.sessions.add(session);

编辑3 ::: 这是我的课程课程

@Entity
public class Course extends Model {
public String courseName;
public String courseCode;

@Lob
public String courseDescription;

@ManyToOne
public Department department;
public Integer capacity;

@OneToMany
public List<Session> sessions = new ArrayList<Session>();

public Course(String courseName, String courseCode, String courseDescription, Department department){
    this.courseName = courseName;
    this.courseCode = courseCode;
    this.courseDescription = courseDescription;
    this.department = department;
}
public void AddSession(Integer capacity){
    Session session = new Session(this, capacity);
    //session.save();
    this.sessions.add(session);
    this.save();
}
public void specifyCapacity(Integer Capacity){
    this.capacity = Capacity;
    this.save();
}
}

编辑4 ::: this.save

public void AddSession(Integer capacity)的新错误
17:27:46,017 DEBUG ~ insert into Course_Session (Course_id, sessions_id) values (?, ?)
17:27:46,059 ERROR ~ 

@6gbd9kpf7
Internal Server Error (500) for request POST /courses/session

Execution exception (In /app/models/Course.java around line 40)
IllegalStateException occured : org.hibernate.TransientObjectException: object references an  unsaved transient instance - save the transient instance before flushing: models.Session

play.exceptions.JavaExecutionException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: models.Session
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: models.Session
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1386)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1323)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:965)
at play.db.jpa.JPABase._save(JPABase.java:41)
at play.db.jpa.GenericModel.save(GenericModel.java:215)
at models.Course.AddSession(Course.java:40)
at controllers.Courses.session(Courses.java:36)
at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
... 1 more
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: models.Session
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:121)
at  org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:815)
at  org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1203)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)
at  org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:345)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:962)
... 10 more

1 个答案:

答案 0 :(得分:0)

尝试添加一个no arg构造函数并在此处初始化您的会话。如果通过播放在场景后面检索实体,则可以使用此construtor初始化,首先使会话为空。

在保存课程之前,请先按照注释代码保存会话。