2

我正在使用 Java 1.7.0_15 和 2.10.0 使用 play 2.1.1 Scala。
错误 ( [NullPointerException: null]) 当我尝试同时访问 a 的内容时,我都跳ArrayListHashMap

我尝试过两种方式:

首先,在构造函数中是初始化属性:

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>>();        
}
....
}   

CountryCar,只有一个String属性。

但是由于用户是使用表单创建的:

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());
}

我决定删除构造函数,因为没有使用,并将 ArrayList 初始化为HashMap属性声明中的:

public Class User extends Model {
    List<Country> countriesVisited = new ArrayList<Country>();
    Map <Country, List<Car>> travels = new HashMap<Country, List<Car>>();
    ...

}

访问时显示在视图中的内容都给了我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));
}

并且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对我来说,同样的事情也会发生。

提前致谢。

这里堆栈跟踪:!@6fbjmmpaa - 内部服务器错误,用于 (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]
4

1 回答 1

1

类在User这里似乎无关紧要。如果countriesVisited为 null,则@for循环已经失败。

该方法ArrayList.contains接受null参数并且永远不会抛出任何参数,NullPointerException除非元素equals方法有问题。

看起来countries简直就是null。检查 的返回值Country.find.all()

于 2013-08-21T11:29:19.247 回答