NoSuchElementException None.get when submitting a form that has errors and an unselected checkbox

495 views
Skip to first unread message

kyle valade

unread,
Sep 8, 2016, 11:46:30 AM9/8/16
to Play Framework
This issue feels a bit like a bug to me or I would have just gone onto StackOverflow, but I could also be doing something stupid. I'm using Play 2.5.4 with Java. 

I'm using FormFactory, and the issue is that the boolean field is for some reason null when I submit the form and there is an error. For example, with the code below, if I omit the name in the form and have isBar unchecked, then the exception will occur. When I debug and look at the generated fooForm.template.scala, the field value is indeed null for isBar. 

However, if the name is filled in, then the exception doesn't occur and the field is correctly false. If I check the box and omit name, then the error message appears as expected.


Here is my code:

```
# The Model

package models;

import com.avaje.ebean.Model;
import play.data.validation.Constraints;


@Entity
public class Foo extends Model {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
@Column(nullable=false, unique=true)
private long id;

@Column(nullable=false)
@Constraints.Required
private String name = "";

    @Column(nullable=false)
    private boolean isBar = false;

    /** Getters/setters */
}
```

```
# The controller

public class FooController extends Controller {

    public Result create() {
        Form<Foo> form = formFactory.form(Foo.class);
form = form.fill(new Foo());
form = form.bindFromRequest();

if (! form.hasErrors()) {
Foo foo = form.get();
foo.save();
return redirect(routes.FooController.update(foo.getId()));
} else {
return badRequest(views.html.foo.create.render(form));
}
}
}
```

```
# The view

@helper.form(action=routes.FooController.create()) {
    <ul class="form-group">
<li>@helper.inputText(form("name"), 'class -> "form-control")</li>
<li>@displayCheckbox(form("isBar"))</li>
    </ul>
    <button type="submit" class="btn btn-primary">Add Foo</button>
}
```

```
# @displayCheckbox tag

@(field: Field)

<label for="@field.name">@field.name</label>
<input type="checkbox" id="@field.name" name="@field.name" value="true"
@if(field.value.get == "true") {
checked="checked"
}
/>
```


Here is the stacktrace:

```

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.util.NoSuchElementException: None.get]]

at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)

at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)

at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)

at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)

at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)

at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)

at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)

at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)

at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)

at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)

Caused by: java.util.concurrent.CompletionException: java.util.NoSuchElementException: None.get

at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)

at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)

at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:593)

at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)

at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)

at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977)

at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:21)

at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:18)

at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)

at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)

Caused by: java.util.NoSuchElementException: None.get

at scala.None$.get(Option.scala:347)

at scala.None$.get(Option.scala:345)

at views.html.tags.forms.displayCheckbox_Scope0$displayCheckbox.apply(displayCheckbox.template.scala:36)

at views.html.foo.fooForm_Scope0$fooForm.apply(fooForm.template.scala:54)

at views.html.foo.create_Scope0$create$$anonfun$apply$1.apply(create.template.scala:41)

at views.html.foo.create_Scope0$create$$anonfun$apply$1.apply(create.template.scala:40)

at views.html.helper.form_Scope0$form.apply(form.template.scala:35)

at views.html.foo.create_Scope0$create.apply(create.template.scala:40)

at views.html.foo.create_Scope0$create.render(create.template.scala:49)

at views.html.foo.create.render(create.template.scala)

```


Thanks for taking a look!

kyle valade

unread,
Sep 8, 2016, 12:55:13 PM9/8/16
to Play Framework
Well, you'll never believe it, but the solution was to upgrade to 2.5.6
Reply all
Reply to author
Forward
0 new messages