在尝试检查 ListView 中特定项目时出现未附加源错误。

huangapple 未分类评论48阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2020年5月4日 22:28:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/61594585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定