Javafx / FXML:initialize()方法与FXML Loader之间的冲突:初始化:NullPointerException,FXML.LoadException

如标题所示,用于构建表并将所有内容添加到表的初始化方法与FXMLLoader之间存在一定的冲突,该FXMLLoader应该为弹出窗口加载FXML。

我的代码:

主要:

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


public class Main extends Application {

    private TableView<Artikel> table;

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

    @Override
    public void start(Stage stage) throws Exception {
    stage.setTitle("Table View Sample");
    stage.setWidth(1250);
    stage.setHeight(1000);

    Parent root = FXMLLoader.load(getclass().getResource("fxml1.fxml"));

    Scene scene1 = new Scene(root,300,300);


    stage.setScene(scene1);
    stage.show();

}

FXML 1。

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.event.actionEvent?>
<?import javafx.geometry.Pos?>
<?import javafx.collections.FXCollections ?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.StackPane?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="150.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="Controller" stylesheets="stylesheet.css" spacing="4" fx:id="idScene" >

<TableView fx:id="idTable" >

</TableView>

 <HBox alignment="TOP_RIGHT">
        <Button fx:id="idNew" text ="Neu" onaction="#onNew"/>

</HBox>



</VBox>

控制器。

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.actionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Modality;
import javafx.stage.Stage;



public class Controller implements Initializable  {

    @FXML
    private Button idNew;
    @FXML
    private TableView idTable;


    public void onNew(actionEvent event) throws IOException {
        Parent popUpFenster = FXMLLoader.load(getclass().getResource("fxml2.fxml"));
        Scene scene2 = new Scene(popUpFenster,500,500);

        Stage stage = new Stage();
        stage.setScene(scene2);

        stage.initModality(Modality.APPLICATION_MODAL);

        stage.showAndWait();
    }

    @Override
    public void initialize(URL url,ResourceBundle rb) {
        TableColumn place = new TableColumn("Platz");
        TableColumn name = new TableColumn("Name");
        TableColumn weight = new TableColumn("Gewicht");
        TableColumn price = new TableColumn("Preis");
        TableColumn amount = new TableColumn("Anzahl");




        idTable.getcolumns().addAll(place,name,weight,price,amount);

        final ObservableList<Artikel> data = FXCollections.observableArrayList(
                new Artikel(1,"Hallo Welt",4,5,3),new Artikel(1,3,4),new Artikel(2,1));                


        //Step : 3#  Associate data with columns  
            place.setCellValueFactory(new PropertyValueFactory<Artikel,Integer>("Platz"));

            name.setCellValueFactory(new PropertyValueFactory<Artikel,String>("Name"));

            weight.setCellValueFactory(new PropertyValueFactory<Artikel,Integer>("Gewicht"));

            price.setCellValueFactory(new PropertyValueFactory<Artikel,Integer>("Preis"));

            amount.setCellValueFactory(new PropertyValueFactory<Artikel,Integer>("Anzahl"));





        //Step 4: add data inside table
            idTable.setItems(data);


    }
    public class Artikel {

        private int Platz;
        private String Name;
        private int Gewicht;
        private int Preis;
        private int Anzahl;


        public Artikel() {
            this.Platz = 0;
            this.Name = "Hallo Welt";
            this.Gewicht= 0;
            this.Preis = 0;
            this.Anzahl = 0;
        }

        public Artikel(int Platz,String Name,int Gewicht,int Preis,int Anzahl) {
            this.Platz = Platz;
            this.Name = Name;
            this.Gewicht = Gewicht;
            this.Preis = Preis;
            this.Anzahl = Anzahl;
        }


        public int getPlatz() {return this.Platz;};
        public String getName () {return this.Name;}
        public int getGewicht() {return this.Gewicht;};
        public int getPreis() {return this.Preis;}
        public int getanzahl() { return this.Anzahl;}
    }

}

fxml2:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.event.actionEvent?>
<?import javafx.geometry.Pos?>
<?import javafx.collections.FXCollections ?>
<?import javafx.geometry.Insets?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="290.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="Controller" stylesheets="stylesheet.css" fx:id="idPopUp">

<HBox alignment="CENTER">
    <Label text="Platz"/>
    <TextField fx:id="idPlace" />
</HBox>


</VBox>

问题::如果您删除初始化或将其注释掉-Neu- 按钮 -PopUp-工作,否则显示表格但按钮弹出不会,似乎有冲突

xiaojiamail 回答:Javafx / FXML:initialize()方法与FXML Loader之间的冲突:初始化:NullPointerException,FXML.LoadException

fxml2.fxml中,您有:fx:controller="Controller"

分配的控制器通过以下方式调用fxml2.fxml
Parent popUpFenster = FXMLLoader.load(getClass().getResource("fxml2.fxml"));

因此fxml2调用Controller,然后Controller调用fxml2 .......

本文链接:https://www.f2er.com/2782813.html

大家都在问