Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 线程内的变量在某个点为null_Java_Multithreading - Fatal编程技术网

Java 线程内的变量在某个点为null

Java 线程内的变量在某个点为null,java,multithreading,Java,Multithreading,我在JavaFX中的onStart方法中的Thread中访问了一个ArrayList。当我试图从另一个线程再次使用由菜单项触发的方法访问数组列表时,它是空的,我不明白为什么 示例程序: package sample; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javaf

我在
JavaFX
中的
onStart
方法中的
Thread
中访问了一个
ArrayList
。当我试图从另一个
线程
再次使用由
菜单项触发的方法访问
数组列表
时,它是空的,我不明白为什么

示例程序:

package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.ArrayList;

public class Main extends Application {

    private ArrayList<Integer> testArray;
    private Thread t;

    @Override
    public void start(Stage primaryStage) throws Exception{
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
        fxmlLoader.setController(new Main());
        Parent root = (Parent)fxmlLoader.load();
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();

        testArray = new ArrayList<>();
        testArray.add(10);
        t = new Thread(() -> {
            System.out.println("Test number is "+testArray.get(0));
        });

        t.start();
    }

    @FXML
    public void menuItemActionMethod(ActionEvent event) {
        Thread t2t = new Thread(() -> {
            System.out.println("Test number is "+testArray.get(0));
        });

        t2t.start();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

我已在FXML中的
Delete
MenuItem
中附加了
menuiteActionMethod
。在应用程序中运行sample并访问Delete应该运行
方法

现在我们终于得到了一个repo,问题只是
menuiteActionMethod
Main
的不同实例上被调用到
start
方法

作为一名JavaFX开发人员,我不能简单地遵循这里的代码流程,但您可以通过完全删除线程来演示这一点:

private String name = "none set";

@Override
public void start(Stage primaryStage) throws Exception{
    name = "set in start";
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
    fxmlLoader.setController(new Main());
    Parent root = (Parent)fxmlLoader.load();
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
}

@FXML
public void menuItemActionMethod(ActionEvent event) {
    System.out.println("In menuItemActionMethod: " + name);
}
点击菜单项时,您将看到:

In menuItemActionMethod: none set
指示对
menuiteActionMethod
的调用是在与调用
start
的对象不同的对象上进行的

下一步:

  • 阅读有关JavaFX对象生命周期的更多信息
  • 了解如何找到将在事件处理程序中使用的
    Main
    实例
  • 理想情况下,将initialization类与event handling类分开,使其更难进入这种情况

除此之外,你不能多次启动一个线程……我试图在方法内部创建一个新线程并启动他,为什么这不起作用?我们不知道,看不到您正在做什么,也不知道它以何种方式不起作用……首先,当我尝试调用返回arraylist的方法时,抛出的
NullPointer
是在数据库上的。事实上,您甚至没有包含堆栈跟踪也没有帮助,也没有添加关于尝试启动线程的部分的混淆两次。恐怕这个问题目前真是一团糟。您应该准确地确定什么是空的,并就此提出一个问题。
private String name = "none set";

@Override
public void start(Stage primaryStage) throws Exception{
    name = "set in start";
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
    fxmlLoader.setController(new Main());
    Parent root = (Parent)fxmlLoader.load();
    primaryStage.setTitle("Hello World");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
}

@FXML
public void menuItemActionMethod(ActionEvent event) {
    System.out.println("In menuItemActionMethod: " + name);
}
In menuItemActionMethod: none set