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

时间:2016-11-17 12:13:05

标签: java javafx nullpointerexception tableview fxml

我想在我的Tabel(personTable {}}中添加一列,但是当我设置Personen_detailController时,它会告诉我ObservableList<>。我需要的人在NullPointerException中,当我List时,它给了我正确的参数。

我不知道我做错了什么,因为在我System.out.println(per.getPersonId())我做同样的事情并且它正在发挥作用。

我会发布所有必要的课程。感谢您的帮助。

PersonenController:

PersonenController
第196行中的

我调用了我的Person_detailController类(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); } } )。

Personen_detailController:

detailController.openNewPersonTab(item);

错误出现在第69行(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); } } )。

至少是错误:

personTable.setItems(personList);

personen_detail.fxml:

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)

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

1 个答案:

答案 0 :(得分:1)

在加载相应的FXML文件时,@FXML初始化在控制器中通过FXMLLoader注入初始化的字段。

在您的代码中,您自己创建了一个控制器类的实例:

Personen_detailController detailController = new Personen_detailController();

由于加载FXML时detailController未创建FXMLLoader,因此@FXML注入的字段(例如personTable)将不会被注入,并且保持为空。因此空指针异常。 (换句话说,detailController实际上并不是一个控制器,它只是一个碰巧与其中一个FMXL文件的控制器相同的类。)

您尚未发布足够的代码来提供完整的响应,但您需要在加载FXML文件后从FXMLLoader获取控制器:

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),以便两个控制器都可以访问共享模型。