Javafx/FXML:initialize()方法和FXML加载程序之间的冲突:initialize:NullPointerException,FXML.LoadException
正如标题所述,构建表并将所有内容添加到表中的initialize方法与FXMLLoader之间存在一定的冲突,FXMLLoader应该为弹出窗口加载FXML 我的代码: 主要内容:Javafx/FXML:initialize()方法和FXML加载程序之间的冲突:initialize:NullPointerException,FXML.LoadException,java,javafx,model-view-controller,initialization,fxml,Java,Javafx,Model View Controller,Initialization,Fxml,正如标题所述,构建表并将所有内容添加到表中的initialize方法与FXMLLoader之间存在一定的冲突,FXMLLoader应该为弹出窗口加载FXML 我的代码: 主要内容: import javafx.fxml.FXMLLoader; import javafx.application.Application; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; publi
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();
}
导入javafx.fxml.fxmloader;
导入javafx.application.application;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.stage.stage;
公共类主扩展应用程序{
私有表视图表;
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
public void start(Stage)引发异常{
stage.setTitle(“表格视图示例”);
舞台设置宽度(1250);
舞台设置高度(1000);
父根=FXMLLoader.load(getClass().getResource(“fxml1.fxml”);
场景scene1=新场景(根,300300);
第二阶段(场景1);
stage.show();
}
fxml1
<?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,"Hallo Welt",5,3,4),
new Artikel(2,"Hallo Welt",4,3,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;}
}
}
import java.io.IOException;
导入java.net.URL;
导入java.util.ResourceBundle;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.event.ActionEvent;
导入javafx.fxml.fxml;
导入javafx.fxml.fxmloader;
导入javafx.fxml.Initializable;
导入javafx.scene.Parent;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.TableView;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.stage.model;
导入javafx.stage.stage;
公共类控制器实现可初始化{
@FXML
新的私人按钮;
@FXML
私有TableView-idTable;
public void onNew(ActionEvent事件)引发IOException{
父popUpFenster=FXMLLoader.load(getClass().getResource(“fxml2.fxml”);
场景场景2=新场景(popUpFenster,500500);
阶段=新阶段();
第二阶段(场景2);
阶段.初始模态(模态.应用\模态);
stage.show和wait();
}
@凌驾
公共void初始化(URL、ResourceBundle rb){
TableColumn place=新的TableColumn(“Platz”);
TableColumn名称=新的TableColumn(“名称”);
TableColumn重量=新TableColumn(“Gewicht”);
TableColumn价格=新的TableColumn(“Preis”);
TableColumn金额=新TableColumn(“Anzahl”);
idTable.getColumns().addAll(地点、名称、重量、价格、金额);
最终ObservableList数据=FXCollections.observableArrayList(
新Artikel(1,“你好”,4,5,3),
新Artikel(1,“你好”,5,3,4),
新Artikel(2,“你好”,4,3,1));
//步骤:3#将数据与列关联
地点:setCellValueFactory(新物业价值工厂(“Platz”);
名称。setCellValueFactory(新属性ValueFactory(“名称”);
重量。setCellValueFactory(新属性ValueFactory(“Gewicht”);
价格。setCellValueFactory(新的PropertyValueFactory(“Preis”);
金额:setCellValueFactory(新财产价值工厂(“Anzahl”);
//步骤4:在表中添加数据
idTable.setItems(数据);
}
公共级Artikel{
私人国际广场;
私有字符串名称;
私人国际酒店;
私家侦探;
安扎尔私人酒店;
公共艺术{
这个.Platz=0;
this.Name=“你好”;
这个.Gewicht=0;
这个.Preis=0;
这是Anzahl=0;
}
公共Artikel(int Platz、字符串名称、int Gewicht、int Preis、int Anzahl){
这个.Platz=Platz;
this.Name=Name;
this.Gewicht=Gewicht;
this.Preis=Preis;
这个。Anzahl=Anzahl;
}
public int getPlatz(){返回this.Platz;};
公共字符串getName(){返回this.Name;}
public int getGewicht(){返回this.Gewicht;};
public int getPreis(){返回this.Preis;}
public int getAnzahl(){返回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>
问题:如果删除initialize或将其注释掉-Neu-按钮弹出-工作,如果没有-表格显示但按钮弹出不显示,则在
fxml2.fxml
中似乎存在冲突:fx:controller=“controller”
分配的控制器通过以下方式调用
fxml2.fxml
Parent-popUpFenster=fxmloader.load(getClass().getResource(“fxml2.fxml”);
因此
fxml2
调用Controller
,然后Controller
调用fxml2
。与您的问题无关:请学习java命名约定并坚持这些约定plus:a需要错误的完整堆栈跟踪。没有,只是猜测:没有冲突(您可以通过重新命名接口不需要的成员来解决这个问题)-但是您的fxml(或代码,取决于透视图:)中有一个命名错误。啊..fxml和控制器类之间有一个严格的关系:一个关系-您不能在不同的视图中重用同一个控制器(因为每一个只注入它知道的部分,另一个注入的字段为空)谢谢!我把fxml2换成了另一个控制器,它成功了!谢谢!!!