我想将特定数据从一个列表复制到另一个列表。它显示
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);
}
}
答案 0 :(得分:1)
List<menu> selectedMenu = new ArrayList<>();
。在对列表执行操作之前,必须先初始化列表。
答案 1 :(得分:0)
列出selectedMenu = new ArrayList&lt;&gt;();