I'm working with play 2.1.1 Scala using Java 1.7.0_15 and 2.10.0.
The error ( [NullPointerException: null]
) I jump both when I try to access the contents of an ArrayList
as both a HashMap
.
I've tried it in two ways:
First, in the constructor is initialized attributes:
import java.util.ArrayList;
import java.util.HashMap;
public Class User extends Model {
...
public User(...) {
...
countriesVisited = new ArrayList<Country>();
travels = new HashMap<Country, List<Car>>();
}
....
}
Classes Country
and Car
, only have a String
attribute.
But as the Users were created using a form:
public static Result create() {
Form<User> formUser = form(User.class);
return ok(createUSer.render(formUser));
}
public static Result save() {
Form<User> formUSer = form(User.class)
.bindFromRequest();
if (formUser.hasErrors()) {
return badRequest(createUser.render(formUser));
}
User u = formUser.get();
u.save();
return redirect(routes.Application.index());
}
I decided to remove the constructor because not used, and initialize the ArrayList as the HashMap
in the declaration of the attributes:
public Class User extends Model {
List<Country> countriesVisited = new ArrayList<Country>();
Map <Country, List<Car>> travels = new HashMap<Country, List<Car>>();
...
}
both when accessed the content to display in a view gives me NullPointerException: null
public static Result getCountriesVisited() {
User u = User.find.byId(request().username());
ArrayList<Country> countries = Country.find.all();
return ok(showCountriesVisited.render(u.countriesVisited, countries));
}
And showCountriesVisited.scala.html
:
@(countriesVisited: List[Country], countries : List[Country])
@import helper._
@import helper.twitterBootstrap
@main("Mark all the countries visited"){
@form(routes.AdminUser.updateCountries()){
@for(cv <- countries){
@if(countriesVisited.contains(cv)){ //THE ERROR ALWAYS SHOWS ME THIS LINE
<input type='checkbox' name='countriesVisited' value=@cv
checked="yes">@cv.name<br>
} else {
<input type='checkbox' name='countriesVisited' value=@cv>@cv.name<br>
}
}
<input type="submit" value="Save">
}
}
HashMap
for me the same thing happens.
Thanks in advance.
Here Stack Trace: ! @6fbjmmpaa - Internal server error, for (GET) [/UserTravel] ->
play.api.Application$$anon$1: Execution exception[[NullPointerException: null]]
at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.1]
at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:144) [play_2.10.jar:2.1.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:140) [play_2.10.jar:2.1.1]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.1]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) [play_2.10.jar:2.1.1]
at play.api.libs.concurrent.PlayPromise$$anonfun$extend$1$$anonfun$apply$1.apply(Promise.scala:104) [play_2.10.jar:2.1.1]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) [scala-library.jar:na]
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) [scala-library.jar:na]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.7.0_15]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.7.0_15]
at java.lang.Thread.run(Unknown Source) [na:1.7.0_15]
java.lang.NullPointerException: null
at views.html.showCountriesVisited$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$3.apply(showCountriesVisited.template.scala:39) ~[na:na]
at views.html.showCountriesVisited$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$3.apply(showCountriesVisited.template.scala:38) ~[na:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
at scala.collection.Iterator$class.foreach(Iterator.scala:727) ~[scala-library.jar:na]
at scala.collection.AbstractIterator.foreach(Iterator.scala:1156) ~[scala-library.jar:na]
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72) ~[scala-library.jar:na]
at scala.collection.AbstractIterable.foreach(Iterable.scala:54) ~[scala-library.jar:na]
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) ~[scala-library.jar:na]
at scala.collection.AbstractTraversable.map(Traversable.scala:105) ~[scala-library.jar:na]
at views.html.showCountriesVisited$$anonfun$apply$1$$anonfun$apply$2.apply(showCountriesVisited.template.scala:38) ~[na:na]
at views.html.showCountriesVisited$$anonfun$apply$1$$anonfun$apply$2.apply(showCountriesVisited.template.scala:36) ~[na:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
at scala.collection.Iterator$class.foreach(Iterator.scala:727) ~[scala-library.jar:na]
at scala.collection.AbstractIterator.foreach(Iterator.scala:1156) ~[scala-library.jar:na]
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72) ~[scala-library.jar:na]
at scala.collection.AbstractIterable.foreach(Iterable.scala:54) ~[scala-library.jar:na]
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) ~[scala-library.jar:na]
at scala.collection.AbstractTraversable.map(Traversable.scala:105) ~[scala-library.jar:na]
at views.html.showCountriesVisited$$anonfun$apply$1.apply(showCountriesVisited.template.scala:36) ~[na:na]
at views.html.showCountriesVisited$$anonfun$apply$1.apply(showCountriesVisited.template.scala:34) ~[na:na]
at views.html.helper.form$.apply(form.template.scala:45) ~[play_2.10.jar:2.1.1]
at views.html.showCountriesVisited$.apply(showCountriesVisited.template.scala:34) ~[na:na]
at views.html.showCountriesVisited$.render(showCountriesVisited.template.scala:58) ~[na:na]
at views.html.showCountriesVisited.render(showCountriesVisited.template.scala) ~[na:na]
at controllers.User.getCountriesVisited(User.java:69) ~[na:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$15$$anonfun$apply$15.apply(routes_routing.scala:258) ~[na:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$15$$anonfun$apply$15.apply(routes_routing.scala:258) ~[na:na]
at play.core.Router$HandlerInvoker$$anon$6$$anon$2.invocation(Router.scala:164) ~[play_2.10.jar:2.1.1]
at play.core.Router$Routes$$anon$1.invocation(Router.scala:345) ~[play_2.10.jar:2.1.1]
at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:31) ~[play_2.10.jar:2.1.1]
at play.GlobalSettings$1.call(GlobalSettings.java:63) ~[play_2.10.jar:2.1.1]
at play.mvc.Security$AuthenticatedAction.call(Security.java:39) ~[play_2.10.jar:2.1.1]
at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:74) ~[play_2.10.jar:2.1.1]
at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:73) ~[play_2.10.jar:2.1.1]
at play.libs.F$Promise$PromiseActor.onReceive(F.java:420) ~[play_2.10.jar:2.1.1]
at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:159) ~[akka-actor_2.10.jar:na]
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:425) ~[akka-actor_2.10.jar:na]
at akka.actor.ActorCell.invoke(ActorCell.scala:386) ~[akka-actor_2.10.jar:na]
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:230) ~[akka-actor_2.10.jar:na]
at akka.dispatch.Mailbox.run(Mailbox.scala:212) ~[akka-actor_2.10.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:502) ~[akka-actor_2.10.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:262) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1478) ~[scala-library.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104) ~[scala-library.jar:na]
The User
class seems irrelevant here. If countriesVisited
were null, the @for
loop would already fail.
The method ArrayList.contains
accepts null
arguments and won't ever throw any NullPointerException
unless there's something wrong with the elements equals
methods.
It looks like countries
simply is null
. Check the return value of Country.find.all()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.