gpt4 book ai didi

Java NullPointerException TavleView.setItem(FXCollections.observableArrayList())

转载 作者:行者123 更新时间:2023-12-01 09:14:38 27 4
gpt4 key购买 nike

我想在我的表格中添加一列(personTable中的Personen_detailController),但是当我设置ObservableList<>时它告诉我NullPointerException 。我需要的人在 List 中当我这样做时,它给了我正确的参数 System.out.println(per.getPersonId())

我不知道我做错了什么,因为在我的 PersonenController 中我做了同样的事情并且它有效。

我会发布每门必要的类(class)。感谢您的帮助。

人员 Controller :

package dummy.dummy.dummy.personen;

import dummy.dummy.InformationDialog;
import dummy.model.Person;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.Callback;

/**
* FXML Controller class
*
* @author z0035ddc
*/
public class PersonenController implements Initializable {

String filterText = null;

ArrayList<Person> PerList;
ObservableList<Person> personSuchList = FXCollections.observableArrayList();

Personen_detailController detailController = new Personen_detailController();


@FXML
TableView<Person> personSuchTable;


@FXML
TextField Name_TextFeld;
@FXML
TextField Kurzname_TextFeld;


@Override
public void initialize(URL url, ResourceBundle rb) {

}

@FXML
private void search() {

if(!Name_TextFeld.getText().equals("") && Kurzname_TextFeld.getText().equals("")){ // Falls nach Name gesucht wird
filterText = Name_TextFeld.getText();
filterText = filterText.replace("*", "%");
perListByName();
}else if(!Kurzname_TextFeld.getText().equals("") && Name_TextFeld.getText().equals("")){ // Falls nach Kurnamen gesucht wird
filterText = Kurzname_TextFeld.getText();
filterText = filterText.replace("*", "%");
perListByShortname();
}else if(Name_TextFeld.getText().equals("") && Kurzname_TextFeld.getText().equals("")){ // Falls Textfelder nicht befüllt
InformationDialog dia = new InformationDialog(Alert.AlertType.INFORMATION, "Bitte geben Sie einen Namen bzw. Kurznamen ein.");
}else if(!Name_TextFeld.getText().equals("") && !Kurzname_TextFeld.getText().equals("")){ // Falls beide Textfelder befüllt
InformationDialog dia = new InformationDialog(Alert.AlertType.INFORMATION, "Es dürfen nicht gleichzeitig Name und Kurzname gefüllt sein.");
}

/*****************************FEHLER!!!***************************/

if(!PerList.isEmpty()){

personSuchList.clear();
personSuchTable.getColumns().clear();

for (Person per : PerList) {
personSuchList.add(per);
}

personSuchTable.setItems(personSuchList);
initTable();


}else{

InformationDialog dia = new InformationDialog(Alert.AlertType.INFORMATION, "Person wurde nicht gefunden.");

}

}

private void perListByName(){
PerList = new ArrayList<>();
PerList = Person.getListPersonenAndDoQueryByName(filterText);
}

private void perListByShortname(){
PerList = new ArrayList<>();
PerList = Person.getListPersonenAndDoQueryByShortname(filterText);
}

@FXML
private void searchByEnter(KeyEvent event){
if (event.getCode() == KeyCode.ENTER || event == null) {
search();
}
}

@FXML
private void initTable() {
//Erste Spalte
TableColumn personId = new TableColumn();
personId.setText("Person_ID");
personId.setMinWidth(50);
personId.setCellValueFactory(new PropertyValueFactory("personId"));
personSuchTable.getColumns().add(personId);

//Zweite Spalte

TableColumn kurzname = new TableColumn();
kurzname.setText("Kurzname");
kurzname.setMinWidth(50);
kurzname.setCellValueFactory(new PropertyValueFactory("kurzname"));
personSuchTable.getColumns().add(kurzname);

//dritte Spalte

TableColumn name = new TableColumn();
name.setText("NAME");
name.setMinWidth(100);
name.setCellValueFactory(new PropertyValueFactory("name"));
personSuchTable.getColumns().add(name);

//vierte Spalte

TableColumn Land = new TableColumn();
Land.setText("Land");
Land.setMinWidth(50);
Land.setCellValueFactory(new PropertyValueFactory("land"));
personSuchTable.getColumns().add(Land);

//fünfte Spalte

TableColumn sperre = new TableColumn();
sperre.setText("Gesperrt");
sperre.setMinWidth(5);
sperre.setCellValueFactory(new PropertyValueFactory("sperre"));
personSuchTable.getColumns().add(sperre);

//fünfte Spalte

TableColumn<Person, Boolean> detail = new TableColumn<>();
detail.setText("");
detail.setMinWidth(150);
Callback<TableColumn<Person, Boolean>, TableCell<Person, Boolean>> detailsColumnCellFactory = //
new Callback<TableColumn<Person, Boolean>, TableCell<Person, Boolean>>() {

@Override
public TableCell call(final TableColumn param) {
final TableCell cell = new TableCell() {

@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
final Button btnDetail = new Button("Details");
btnDetail.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
param.getTableView().getSelectionModel().select(getIndex());
Person item = personSuchTable.getSelectionModel().getSelectedItem();
if (item != null) {

detailController.openNewPersonTab(item);
}
}
});
setGraphic(btnDetail);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
}
}
};
return cell;
}
};
detail.setCellFactory(detailsColumnCellFactory);
personSuchTable.getColumns().add(detail);
}
}

在第 196 行,我调用我的 Person_detailController 类 ( detailController.openNewPersonTab(item); )。

Personen_detailController:

package admintool.ui.stammdaten.personen;

import admintool.AdminTool;
import admintool.helpers.ExceptionDialog;
import admintool.model.Person;
import admintool.ui.steuerdaten.benutzerverwaltung.BenutzerSucheController;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Alert;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

/**
*
* @author z0035ddc
*/
public class Personen_detailController {

Person per;

@FXML
TableView<Person> personTable;

ObservableList<Person> personList = FXCollections.observableArrayList();

@FXML
TextField name_TextFeld_detail;
@FXML
TextField land_TextFeld_detail;
@FXML
TextField personId_TextFeld_detail;
@FXML
TextField sperre_TextFeld_detail;
@FXML
TextField windowsKennung_TextFeld_detail;

@FXML
public void openNewPersonTab(Person per) {
try {
//System.out.println(per.getName());
FXMLLoader loader =new FXMLLoader(getClass().getResource("/admintool/ui/stammdaten/personen/personen_detail.fxml"));

TabPane tp = AdminTool.getRootlayoutPane();
Tab newTab = new Tab(per.getName());
newTab.setContent(loader.load());
tp.getTabs().add(newTab);
tp.getSelectionModel().select(newTab);



personList.add(per);

System.out.println(per.getPersonId());

personTable.setItems(personList);

Personen_detailController controller = loader.<Personen_detailController>getController();
controller.initData(per);

} catch (IOException ex) {
Logger.getLogger(BenutzerSucheController.class.getName()).log(Level.SEVERE, null, ex);
ExceptionDialog dia = new ExceptionDialog(Alert.AlertType.ERROR, ex);

}
}

@FXML
private void initData(Person per) {
this.per = per;

this.name_TextFeld_detail.setText(per.getName());
this.land_TextFeld_detail.setText(per.getLand());

this.personId_TextFeld_detail.setText(per.getPersonId().toString());
this.sperre_TextFeld_detail.setText(per.getSperre().toString());
this.windowsKennung_TextFeld_detail.setText(per.getOsKennung());

//Erste Spalte
/* TableColumn vertragspartnerColumn = new TableColumn();
vertragspartnerColumn.setText("Vertragspartner");
vertragspartnerColumn.setMinWidth(50);
vertragspartnerColumn.setCellValueFactory(new PropertyValueFactory("vertragspartner"));
personTable.getColumns().add(vertragspartnerColumn);*/

//Erste Spalte
TableColumn personId = new TableColumn();
personId.setText("Person_ID");
personId.setMinWidth(50);
personId.setCellValueFactory(new PropertyValueFactory("personId"));
personTable.getColumns().add(personId);
}

}

错误出现在第 69 行 ( personTable.setItems(personList); )。

至少还有错误:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at dummy.dummy.dummy.personen.Personen_detailController.openNewPersonTab(Personen_detailController.java:69)
at dummy.dummy.dummy.personen.PersonenController$1$1$1.handle(PersonenController.java:196)
at dummy.dummy.dummy.personen.PersonenController$1$1$1.handle(PersonenController.java:188)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

personen_detail.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.net.URL?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="882.0" prefWidth="1294.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="admintool.ui.stammdaten.personen.Personen_detailController">
<stylesheets>
<URL value="@personen.css" />
</stylesheets>
<children>
<TabPane fx:id="tabPane" prefHeight="882.0" prefWidth="1294.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab closable="false" text="Details">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<SplitPane dividerPositions="0.2291421856639248" layoutX="151.0" layoutY="82.0" orientation="VERTICAL" prefHeight="853.0" prefWidth="1294.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Name:" />
<Label layoutX="566.0" layoutY="49.0" text="Sperre:" />
<Label layoutX="547.0" layoutY="14.0" text="Person_ID:" />
<TextField fx:id="name_TextFeld_detail" layoutX="66.0" layoutY="10.0" prefHeight="25.0" prefWidth="186.0" />
<Label layoutX="20.0" layoutY="49.0" text="Land:" />
<TextField fx:id="land_TextFeld_detail" layoutX="66.0" layoutY="45.0" prefHeight="25.0" prefWidth="186.0" />
<TextField fx:id="personId_TextFeld_detail" layoutX="620.0" layoutY="10.0" />
<Label layoutX="500.0" layoutY="88.0" text="Windows Kennung:" />
<TextField fx:id="sperre_TextFeld_detail" layoutX="620.0" layoutY="45.0" />
<TextField fx:id="windowsKennung_TextFeld_detail" layoutX="620.0" layoutY="84.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TableView fx:id="personTable" prefHeight="709.0" prefWidth="1294.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>

如果您需要任何帮助,请告诉我。谢谢大家。

最佳答案

在 Controller 中通过@FXML注入(inject)初始化的字段在加载相应的FXML文件时由FXMLLoader初始化。

在您的代码中,您自己创建 Controller 类的实例:

Personen_detailController detailController = new Personen_detailController();

由于在加载 FXML 时 detailController 不是由 FXMLLoader 创建的,因此 @FXML 注入(inject)的字段(例如 personTable) 不会被注入(inject),并且将保持为空。因此出现空指针异常。 (换句话说,detailController 实际上并不是一个 Controller ,它只是一个恰好与 FMXL 文件之一的 Controller 属于同一类的对象。)

您还没有发布足够的代码来给出完整的响应,但您需要在加载 FXML 文件后从 FXMLLoader 获取 Controller :

FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/fxml/file"));
Parent root = loader.load();
// display root in UI
Person_detailController detailController = loader.getController();

然后安排将 detailController 传递给 PersonenController 实例,该实例同样是从其 FXMLLoader 检索的。

作为替代方案,请考虑使用 MVC 方法(例如 Applying MVC With JavaFx ),以便两个 Controller 都可以访问共享模型。

关于Java NullPointerException TavleView.setItem(FXCollections.observableArrayList()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40654589/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com