小编Tim*_*ron的帖子

JavaFX,标签空指针异常

我目前正在编写的程序遇到以下问题,我在互联网上进行了搜索,但我找不到任何东西可以帮助我理解以下问题

因此,在另一个类中,我编写了一个方法,每当单击搜索按钮时都会执行此方法,该方法如下所示:

public void searchButton(){
        try {
            new SearchController().display();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后 SearchController 类看起来像这样(我在这里简化了它):

public class SearchController {

    @FXML
    private Button cancelButton;

    @FXML
    private Label what;

    private static Stage stage;

    private static BorderPane borderPane;

    @FXML
    public void initialize(){
        what.setText("Testing"); // this woks
        cancelButton.setOnAction(e -> stage.close());
    }

    public void display() throws IOException {

        stage = new Stage();
        stage.setResizable(false);
        stage.setTitle("Product search");
        stage.initModality(Modality.APPLICATION_MODAL);
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(SearchController.class.getResource("Search.fxml"));
        borderPane = loader.load();
        Scene scene = new Scene(borderPane); …
Run Code Online (Sandbox Code Playgroud)

label javafx nullpointerexception

3
推荐指数
1
解决办法
8962
查看次数

标签 统计

javafx ×1

label ×1

nullpointerexception ×1