Xplosio Xplosio - 18 days ago 10
Java Question

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

I want to add in my Tabel (

personTable
in
Personen_detailController
) a column but when I set the
ObservableList<>
it tells me
NullPointerException
. The person, which I need is in the
List
and it gives me the right parameter when I do
System.out.println(per.getPersonId())
.

I don't know what I am doing wrong because in my
PersonenController
I do the same thing and it's working.

I will post every class that is necessary. I appreciate your help.

PersonenController:

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);
}
}


in line 196 I call my Person_detailController class (
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);
}

}


the error appears in line 69 (
personTable.setItems(personList);
).

and at least the error:

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>


if you need anything to help me, please let me know. Thank you all.

Answer

Fields that are initialized via @FXML injection in a controller are initialized by the FXMLLoader when the corresponding FXML file is loaded.

In your code you are creating an instance of the controller class yourself:

Personen_detailController detailController = new Personen_detailController();

Since detailController is not created by the FXMLLoader while the FXML is loaded, the @FXML-injected fields (such as personTable) will not be injected, and will remain null. Hence the null pointer exception. (In other words, detailController is not actually a controller, it is just an object that happens to be the same class as the controller for one of the FMXL files.)

You haven't posted enough code to give a complete response, but you need to get the controller from the FXMLLoader after the FXML file is loaded:

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

and then arrange for the detailController to be passed to the PersonenController instance that is similarly retrieved from its FXMLLoader.

As an alternative, consider using a MVC approach (e.g. Applying MVC With JavaFx), so both controllers have access to a shared model.

Comments