Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法将在操作集中创建的Arraylist传递给另一个操作集事件处理程序_Java_Eclipse_Javafx - Fatal编程技术网

Java 无法将在操作集中创建的Arraylist传递给另一个操作集事件处理程序

Java 无法将在操作集中创建的Arraylist传递给另一个操作集事件处理程序,java,eclipse,javafx,Java,Eclipse,Javafx,我有两个按钮需要访问同一个数组,它给出了一个错误局部变量newCards,在封闭范围内定义的变量必须是final或有效final 我是否做错了什么,比如违反java规则 这是我迄今为止编写的代码: final ArrayList<Integer> newDeck = new ArrayList<Integer>(); ShuffleFunctions.shuffleDeck(newDeck); int number = Integer.parse

我有两个按钮需要访问同一个数组,它给出了一个错误局部变量newCards,在封闭范围内定义的变量必须是final或有效final

我是否做错了什么,比如违反java规则

这是我迄今为止编写的代码:

    final ArrayList<Integer> newDeck = new ArrayList<Integer>();
    ShuffleFunctions.shuffleDeck(newDeck);

    int number = Integer.parseInt(cardNbr.getText());
     ArrayList<Integer> newCards = new ArrayList<Integer>();
    newCards = ShuffleFunctions.randomCardsSelector(number, newDeck);


    submitBtn.setOnAction(e->{

        fetchBtn.setDisable(false);
        submitBtn.setDisable(true);


        for(int i = 0; i<number; i++) {
            int x = newCards.get(i);
            String url = new String("bin/"+ x +".png");
            File file1 = new File(url);
            Image image1 = new Image(file1.toURI().toString());
            ImageView imageView = new ImageView(image1);
            imageView.setFitHeight(90);
            imageView.setFitWidth(90);
            drawnCards.getChildren().add(imageView);
        }
    });



    fetchBtn.setOnAction(e->{
        fetchBtn.setDisable(true);

        ArrayList<Integer> selectedNewCard= new ArrayList<Integer>();
        selectedNewCard = ShuffleFunctions.randomCardsSelector(1, newDeck);

        int s = selectedNewCard.get(0);
        String url = new String("bin/" + s + ".png");
        File file2 = new File(url);
        Image img = new Image(file2.toURI().toString());
        drawnCard.setImage(img);
        drawnCard.setFitHeight(150);
        drawnCard.setFitWidth(150);
    });

    drawCardBtn.setOnAction(e->{
        drawCardBtn.setDisable(false);

        ArrayList<Integer> oneCard = new ArrayList<Integer>();
        oneCard = ShuffleFunctions.randomCardsSelector(1,newCards);

        String url = new String("bin/" /*x*/ + ".png");
        File file3 = new File(url);
        Image image = new Image(file3.toURI().toString());
        drawnCard.setImage(image);
        drawnCards.getChildren().get(0).setStyle("-fx-opacity: 0.5");
    });
final ArrayList newDeck=new ArrayList();
ShuffleFunctions.shuffleDeck(新甲板);
int number=Integer.parseInt(cardNbr.getText());
ArrayList newCards=新的ArrayList();
newCards=shufflfunctions.randomCardsSelector(数字,newDeck);
提交设置操作(e->{
fetchBtn.setDisable(false);
submitBtn.setDisable(真);
对于(int i=0;i{
fetchBtn.setDisable(true);
ArrayList selectedNewCard=新建ArrayList();
selectedNewCard=ShuffleFunctions.randomCardsSelector(1,newDeck);
int s=selectedNewCard.get(0);
字符串url=新字符串(“bin/”+s+.png”);
File file2=新文件(url);
Image img=新图像(file2.toURI().toString());
drawnCard.setImage(img);
图纸安装高度(150);
图纸卡号设置宽度(150);
});
drawCardBtn.设置操作(e->{
drawCardBtn.setDisable(假);
ArrayList oneCard=新的ArrayList();
oneCard=ShuffleFunctions.randomCardsSelector(1,新卡);
字符串url=新字符串(“bin/”/*x*/+.png”);
文件file3=新文件(url);
Image Image=新映像(file3.toURI().toString());
drawnCard.setImage(图像);
drawnCards.getChildren().get(0.setStyle(“-fx不透明度:0.5”);
});

JAVA中的lambda函数不允许这样做。

问题是,您正在多个lambda函数中使用一个变量,该变量将在将来某个时间点执行,并且无法预先确定。
为了防止这种未知行为,JAVA强制将局部变量设置为final,这样它们的状态就不会改变,我们(代码)就可以有可预测的行为


解决此问题的唯一方法是将变量设置为final

那么为什么不将
newCards
设置为final呢?这会在声明后的行上产生一个新的错误:无法分配最终的局部变量newCards。它必须为空,并且不使用复合赋值。不要声明并分配
newCards
单独。谢谢你,Aulis…但是如果你能告诉我原因,这样我以后就可以避免这种问题,那就不用客气了!看看@swayamraina的答案。运行代码后,代码崩溃了,出现以下错误:java.lang.reflect.InvocationTargetException在下面一行:int number=Integer.parseInt(cardNbr.getText());