从列表中随机设置背景按钮

时间:2019-12-20 00:03:43

标签: java javafx nullpointerexception

我试图将背景随机设置为按钮。我创建了一个对ArrayList

public ArrayList<Pair<Image,Integer>> gettingMyImages()
{
    ArrayList<Pair<Image,Integer>> imageList=new ArrayList<>();

    imageList.add(new Pair<Image,Integer>(new Image( getClass().getResource("file:aha.jpg")),1));

    Collections.shuffle(imageList);

    return imageList;
}

然后我尝试将其作为参数传递给BackroundImage:

counterDisplayed=0;
 if (counterDisplayed==0)
        addImageToScene=example.gettingMyImages();

    if (newToggle.isSelected()) {
        counterDisplayed++;
        BackgroundImage backgroundImage = new BackgroundImage(example.gettingMyImages().get(0).getKey(), BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
        Background background = new Background(backgroundImage);
        newToggle.setBackground(background);
    }

但是我得到了NullPointerException。我应该如何正确做?

0 个答案:

没有答案