英文:
Source not attached error when trying to check for certain items in a ListView
问题
String vald = categoryList.getSelectionModel().getSelectedItem();
Triangle triangle = new Triangle(x, y);
if (vald.equals("Bus")) {
triangle.setFill(Color.BLACK);
} else {
triangle.setFill(Color.YELLOW);
}
center.getChildren().add(triangle);
当我选择三个选项中的任何一个时,它都能正常工作。问题是当我启动程序并且在列表中不选择任何内容时,它会抛出空指针异常,并指向 vald.equals("Bus") 那一行和包声明。当我移除对列表视图检查的代码时,它可以正常工作。因此,我想知道在检查 ListView 中的项目时是否漏掉了什么。(我已经尝试使用单选按钮,它可以正常工作,所以我陷入了死胡同。)
英文:
I created a ListView with three options in it and I want to draw a triangle with a certain color depending on what option in the ListView that is selected.
String vald = categoryList.getSelectionModel().getSelectedItem();
Triangle triangle = new Triangle(x,y);
if(vald.equals("Bus")) {
triangle.setFill(Color.BLACK);
}
else {
triangle.setFill(Color.YELLOW);
}
center.getChildren().add(triangle);
It works when I select any of the three options. Problem is when I start up the program and dont select anything in the List, it gives me a nullpointer exception and points to the vald.equals("Bus") and the package declaration. When I remove the code for the listview check it works tho. So I'm wondering if I'm missing something when checking for items in a ListView. (I checked if it worked with radiobuttons instead and it did so I'm at a dead end)
专注分享java语言的经验与见解,让所有开发者获益!
评论