我想将特定数据从一个列表复制到另一个列表。表明
ERROR:
java.lang.NullPointerException
at controllers.WebForms.getMenu(WebForms.java:134)
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(routes_routing.scala:222)
List<menu> allMenus = menu.find.all();
List<menu> selectedMenu=null;
System.out.println("selected menu:"+selectedMenu);//prints: null
int count = 0;
for (models.menu m : allMenus) {
System.out.println("m:"+m);
if (m.r_id == r_id) {
count=1;
System.out.println("r_id:"+m.r_id+" "+m.item);
//prints: r_id:1 Noodles
selectedMenu.add(m);
//throws NULLPointer Exception
System.out.println("selected menu inside loop:"+selectedMenu);
}
}
最佳答案
List<menu> selectedMenu = new ArrayList<>();
。您必须先初始化列表,然后才能对其执行操作。