2013-08-21 45 views
2

我正在使用Java 1.7.0_15和2.10.0來玩play 2.1.1 Scala。
錯誤([NullPointerException: null])當我試圖以HashMap的身份訪問ArrayList的內容時,我都跳轉。PlayFramework [NullPointerException:null]在ArrayList或HashMap中添加項目時

我已經試過了兩種方式:

首先,在構造函數初始化屬性:

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' [email protected] 
        checked="yes">@cv.name<br> 
       } else { 
       <input type='checkbox' name='countriesVisited' [email protected]>@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] 
+0

cv似乎返回空。 –

+0

你說的是正確的,問題是因爲它在這種情況下應該返回false否則返回false。 – Savins

+0

發佈異常的完整堆棧跟蹤。 –

回答

1

User類似乎這裏無關緊要。如果countriesVisited爲空,則@for循環已經失敗。

ArrayList.contains接受null參數,並永遠不會引發任何NullPointerException,除非有一些錯誤的元素equals方法的方法。

它看起來像countries只是null。檢查返回值Country.find.all()

+0

對不起,我寫混淆循環時感到困惑,現在如果這是正確的。問題是'countriesVisited'因爲'countriesVisited = null',它不應該是,並且在開頭初始化它,所以我也寫作'User'對象(從一個表單),我認爲這是錯誤,因爲表單不會使用'ArrayList',這個'User'稍後將使用你已經訪問過的國家('countries')的列表,並存儲在'countriesVisited'中,其次我創建了複選框在循環。 – Savins

+0

我希望這個更清楚,並且原諒與寫的代碼混淆,我糾正了。只有現在,找出爲什麼'User'對象'ArrayList'是空的,而不是看到一個空的'Arraylist(沒有元素)'。 @kapep – Savins

相關問題