urSus
urSus

Reputation: 12759

Play! 2.0 - OneToOne relationship - NullPointerException

I am new to Play, I am trying to accomplish OneToOne relationship between Canteen and OpeningTimes,I suppose 1:1 is what I want. However I am getting a NullPointerException...

@Entity
public class OpeningTimes extends Model {

   @Id
   public Long id;

   @Required
   public String mondayHours;

   @Required
   public String tuesdayHours;

       ...

   @OneToOne
   public Canteen canteen;

}

@Entity
public class Canteen extends Model {

   @Id
   public Long id;

   ...  

   @OneToOne
   public OpeningTimes openingTimes;

}

Here I populate the database

public class Global extends GlobalSettings {

@Override
public void onStart(Application arg0) {
    if(Canteen.finder.findRowCount() == 0) {

        Canteen canteen = new Canteen();
        ...
        canteen.save();


        Canteen canteen1 = new Canteen();
        ...
        canteen1.save();


        OpeningTimes times = new OpeningTimes();
        times.mondayHours = "7:00 - 15:00";
        times.tuesdayHours = "7:00 - 15:00";
        ...
        times.canteen = canteen;
        times.save();


        OpeningTimes times1 = new OpeningTimes();
        times1.mondayHours = "7:00 - 15:00";
        times1.tuesdayHours = "7:00 - 15:00";
        ...
        times1.canteen = canteen1;
        times1.save();

}

show.scala.html

@(canteen: Canteen)

@main("MamHlad | " + canteen.nameShort) {

   <h1>@canteen.nameFull</h1>

   <h2>Opening times</h2>
   ** <p>@canteen.openingTimes.mondayHours</p> **
   <p>@canteen.openingTimes.tuesdayHours</p>
}

Asterixes mark the line where it throws the NullPointerException, here is the stack trace

! @6c6cgmg4b - Internal server error, for request [GET /canteens/2] ->

play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[NullPointerException: null]]
        at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [play_2.9.1.jar:2.0.4]
        at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [play_2.9.1.jar:2.0.4]
        at akka.actor.Actor$class.apply(Actor.scala:318) [akka-actor.jar:2.0.2]
        at play.core.ActionInvoker.apply(Invoker.scala:113) [play_2.9.1.jar:2.0.4]
        at akka.actor.ActorCell.invoke(ActorCell.scala:626) [akka-actor.jar:2.0.2]
        at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197) [akka-actor.jar:2.0.2]
Caused by: java.lang.NullPointerException: null
        at views.html.show$.apply(show.template.scala:37) ~[classes/:na]
        at views.html.show$.render(show.template.scala:105) ~[classes/:na]
        at views.html.show.render(show.template.scala) ~[classes/:na]
        at controllers.Application.show(Application.java:23) ~[classes/:na]
        at Routes$$anonfun$routes$1$$anonfun$apply$5$$anonfun$apply$6.apply(routes_routing.scala:76) ~[classes/:na]
        at Routes$$anonfun$routes$1$$anonfun$apply$5$$anonfun$apply$6.apply(routes_routing.scala:76) ~[classes/:na]

Thanks for help in advance!

Upvotes: 0

Views: 1300

Answers (1)

therealsachin
therealsachin

Reputation: 1684

I see you have not specified the owning side of the OneToOne relation, which is mandatory.

@Entity
public class OpeningTimes extends Model {

   @Id
   public Long id;
      ...

   @OneToOne(mappedBy="openingTimes", cascade=CascadeType.ALL)
   public Canteen canteen;
}

Also I think you need to use, setOpeningTimes method, instead of accessing the property directly. Like this,

canteen.setOpeningTimes(openingTime1);
canteen.save();

Here is an example: https://ebeanorm.svn.sourceforge.net/svnroot/ebeanorm/ebean/trunk/src/test/java/com/avaje/tests/basic/TestMultipleOneToOneIUD.java

Upvotes: 2

Related Questions