I have declared two fxml files and a controller for each one:
rootlayoutcontroller is a controller for rootlayout.fxml
and
overviewcontroller is a controller for overview.fxml
rootlayout has menu bar with file open item, and overviewcontroller has a Draw button.
I have another class called DataStructure. I want to open a file and send the path to overviewcontroller. Then when I click Draw the constructor of DartaStructure shall get instantiated, with the path as parameter. But as soon as I click open, on file chooser open dialog, the program throws an nullpointer for in handleOpen() method.
Sometimes I get Source not found!
In a main class I wrap overview in rootlayout and show the stage.
RootLayoutController:
public class RootLayoutController {
private final Model model;
public RootLayoutController(Model model){
this.model = model;
}
/**
* Opens a FileChooser to let the user select a .z3lgm file to load.
*/
#FXML
private void handleOpen() {
FileChooser fileChooser = new FileChooser();
// Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
"3lgm2 files (*.z3lgm)", "*z3lgm");
fileChooser.getExtensionFilters().add(extFilter);
// Show open file dialog
File file = fileChooser.showOpenDialog(main.getPrimaryStage());
if (file != null) {
path = file.toString();
model.setText(path);//error
}
}
}
OverViewController:
public class OverViewController implements Initializable {
private final Model model;
public OverViewController(Model model){
this.model = model;
}
public void treeTableDraw(ActionEvent event) {
String p = model.getText();
new Controller(p);
drawTable();
numberOfFunctions = dc.getFuncAll().size();
numberOfOrganizations = dc.getSortedAssignedOrg().size();
funcLabel.setText(numberOfFunctions + "");
orgLabel.setText(numberOfOrganizations + "");
btnDraw.setDisable(true);
}
}
Error stack:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
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.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$341(ContextMenuContent.java:1358)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer$$Lambda$326/15872584.handle(Unknown Source)
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.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:3758)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$209/5704663.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$41/19140780.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1767)
... 46 more
Caused by: java.lang.NullPointerException
at view.RootLayoutController.handleOpen(RootLayoutController.java:174)
... 56 more
Main Class:
public class Main extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private Model model = new Model;
// private ObservableList<DataConstructor> treeTableData =
// FXCollections.observableArrayList();
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("IT-Saturation");
initRootLayout();
showOverView();
}
private void showOverView() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/view/OverView.fxml"));
loader.setController(new OverViewController(model));
// Parent firstUI = loader.load();
AnchorPane overView = (AnchorPane) loader.load();
rootLayout.setCenter(overView);
// OverViewController controller = loader.getController();
// controller.setMainApp(this);
} catch (IOException e) {
e.printStackTrace();
}
}
private void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/view/RootLayout.fxml"));
loader.setController(new RootLayoutController(model));
rootLayout = (BorderPane) loader.load();
// show scene containing the root layout
Scene scene = new Scene(rootLayout);
scene.getStylesheets().add(
getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
// gives controller access to main
RootLayoutController controller = loader.getController();
controller.setMainApp(this);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
*
* #return primaryStage
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
public void showMostComputerizedStatistics() {
try {
// Load the fxml file and create a new stage for the popup.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class
.getResource("view/BirthdayStatistics.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("Birthday Statistics");
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(primaryStage);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
// Set the persons into the controller.
MostComputerizedController controller = loader.getController();
dialogStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is RootLayout fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.input.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane prefHeight="500.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" >
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu text="_File">
<items>
<MenuItem onAction="#handleOpen" text="_Open" />
<MenuItem mnemonicParsing="false" onAction="#handleExportPic" text="Export as picture" />
<MenuItem mnemonicParsing="false" text=" Save as" />
<MenuItem mnemonicParsing="false" onAction="#doPrint" text="Print" />
<MenuItem onAction="#handleExit" text="E_xit">
<accelerator>
<KeyCodeCombination alt="UP" code="X" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator></MenuItem>
</items>
</Menu>
<Menu mnemonicParsing="false" onAction="#handleMostComputerizedStatistics" text="Statistics">
<items>
<MenuItem mnemonicParsing="false" onAction="#handleMostComputerizedStatistics" text="Show Statistics" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
And this is overview fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" >
<children>
<SplitPane dividerPositions="0.2391304347826087" layoutX="62.0" layoutY="52.0" prefHeight="400.0" prefWidth="600.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="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
<children>
<Button fx:id="btnDraw" layoutX="24.0" layoutY="297.0" mnemonicParsing="false" onAction="#treeTableDraw" text="Draw" AnchorPane.leftAnchor="22.0" AnchorPane.rightAnchor="36.0" />
<Label fx:id="funcLabel" layoutX="24.0" layoutY="155.0" opacity="0.72" prefHeight="15.0" prefWidth="38.0" text="0" AnchorPane.bottomAnchor="190.0" AnchorPane.leftAnchor="24.0" AnchorPane.rightAnchor="77.0">
<font>
<Font size="10.0" />
</font></Label>
<Label fx:id="orgLabel" layoutX="17.0" layoutY="219.0" opacity="0.72" prefHeight="14.0" prefWidth="38.0" text="0" AnchorPane.bottomAnchor="165.0" AnchorPane.leftAnchor="24.0" AnchorPane.rightAnchor="84.0" AnchorPane.topAnchor="219.0">
<font>
<Font size="10.0" />
</font></Label>
<Label layoutX="63.0" layoutY="155.0" text="Functions" AnchorPane.bottomAnchor="190.0">
<font>
<Font size="11.0" />
</font>
</Label>
<Label layoutX="56.0" layoutY="219.0" text="Organizations" AnchorPane.bottomAnchor="165.0" AnchorPane.topAnchor="213.0">
<font>
<Font size="10.0" />
</font>
</Label>
<Button fx:id="btnReset" layoutX="21.0" layoutY="345.0" mnemonicParsing="false" onAction="#treeTableReset" onKeyPressed="#treeTableReset" prefHeight="25.0" prefWidth="81.0" text="Reset" AnchorPane.leftAnchor="22.0" AnchorPane.rightAnchor="36.0" />
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" SplitPane.resizableWithParent="false">
<children>
<ScrollPane fx:id="scrollPane" prefHeight="398.0" prefWidth="480.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<TreeTableView fx:id="treeTable" editable="true" prefHeight="375.0" prefWidth="246.0" />
</content>
</ScrollPane>
</children></AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
FXMLLoader.getController() will return null if you call it before the load() method has been called (because if it hasn't loaded the FXML file, it doesn't know what the controller is).
But this is still not going to do what you want. You want a reference to the controller instance that is associated with the root of the FMXL file that is displayed in the UI. It doesn't help to just get an arbitrary controller instance, whether you do it by getting it from another FXMLLoader or by instantiating it directly.
The controller instance you want is the one you can get by calling loader.getController() in the showOverview() method in your Main class. If you want the controllers to communicate with each other, you need to arrange for the RootLayoutController instance to have a reference to that OverViewController instance.
It might be better not to have the controllers have direct access to each other, but to let them communicate with data in a shared data model. This question has a simple example of this approach.
Related
Introduction:
This is simply a "Table" where cards can be "placed" on, by right clicking on the table.
I have 2 FXML objects:
Card
UI
Problem:
In short, a Card needs to be added to the UI "Table", this is where my problem lies.
The adding Card method simply adds the card to a Pane object (which is not null), and we know that Pane's add objects which extend a Node
I tried 2 solutions, of which both failed.
Solution 1
Using the FXMLLoader to load the Card.fxml file into a Card object:
Having the Card definition as follows
public class Card implements Initializable
causes issues:
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Layouts/Card.fxml"));
Card card = loader.load();
card.setLayoutX(mouseEvent.getSceneX() - (card.getLayoutX() / 2)); <--------here setLayoutX
card.setLayoutY(mouseEvent.getSceneY() - (card.getLayoutY() / 2)); <--------here setLayoutY
card.isFaceDown.addListener((observable, oldValue, newValue) -> {
if (newValue) {
facedownCount.add(1);
} else {
facedownCount.subtract(1);
}
});
totalCount.add(1);
gamePane.getChildren().add(card); <--------here card does not extend Node
The issue is I am unable to set layout properties of the Card object if it doesn't extend a stackpane, and also I am unable to add it to another object as a child
Solution 2: (more likely to be correct)
Changing the Card definition to
public class Card extends StackPane implements Initializable
followed by attempting to add it:
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Layouts/Card.fxml"));
Card card = loader.load(); < -------- PROBLEM
which gives me this exception error:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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.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:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
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.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 29 more
Caused by: java.lang.ClassCastException: javafx.scene.layout.StackPane cannot be cast to Game.Controller.Card
at Game.Controller.UI.HandleClick(UI.java:60)
... 39 more
Card.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.effect.DropShadow?>
<StackPane maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.112"
fx:controller="Game.Controller.Card"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fx:id="imgSuit" fitHeight="275.0" fitWidth="183.0" layoutX="10.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="imgValue" fitHeight="275.0" fitWidth="183.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../suits/back.png" />
</image>
</ImageView>
</children>
<effect>
<DropShadow />
</effect>
</StackPane>
Card Controller
public class Card implements Initializable {
public static HashMap cardSuit, cardValue;
public BooleanProperty isFaceDown = new SimpleBooleanProperty(true);
private IntegerProperty value = new SimpleIntegerProperty(-1);
public StringProperty suit = new SimpleStringProperty("");
public ImageView imgValue = new ImageView();
public ImageView imgSuit = new ImageView();
public Card(){
super();
}
#Override
public void initialize(URL location, ResourceBundle resources) {
initImages();
setupBindings();
setDefaults();
}
private void setDefaults() {
isFaceDown.set(true);
}
private void setupBindings() {
isFaceDown.addListener((observable, oldValue, newValue) -> {
if (newValue){
imgValue.setImage(null);
imgSuit.setImage((Image) cardSuit.get("B"));
}
else {
imgValue.setImage((Image) cardValue.get(value.get()));
imgSuit.setImage((Image) cardSuit.get(suit.get()));
}
});
}
private void initImages() {
if (cardSuit == null) {
cardSuit = new HashMap<String, Image>();
cardSuit.put("B", new Image("/suits/back.png"));
cardSuit.put("C", new Image("/suits/clubs.png"));
cardSuit.put("D", new Image("/suits/diamonds.png"));
cardSuit.put("S", new Image("/suits/spades.png"));
cardSuit.put("H", new Image("/suits/hearts.png"));
}
if (cardValue == null) {
cardValue = new HashMap<Integer, Image>();
for (int i = 1; i <= 13; i++) {
cardValue.put(i, new Image("/values/" + String.valueOf(i) + ".png"));
}
}
}
}
UI fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMousePressed="#HandleClick" prefHeight="750.0" prefWidth="702.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Game.Controller.UI">
<children>
<MenuBar prefHeight="29.0" prefWidth="700.0">
<menus>
<Menu mnemonicParsing="false" text="Menu">
<items>
<MenuItem fx:id="mmReset" mnemonicParsing="false" text="Reset" />
<MenuItem fx:id="mmClose" mnemonicParsing="false" onAction="#CloseApp" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem fx:id="mmAbout" mnemonicParsing="false" text="About" />
</items>
</Menu>
<Menu fx:id="mInstruct" mnemonicParsing="false" text="Instructions" />
</menus>
</MenuBar>
<StackPane fx:id="gamePane" layoutX="1.0" layoutY="29.0" prefHeight="700.0" prefWidth="700.0" style="-fx-background-color: green;" />
<HBox layoutX="7.0" layoutY="731.0" prefHeight="15.0" prefWidth="693.0">
<children>
<Label fx:id="lblHearts" text="Hearts = " />
<Label fx:id="lblDiamonds" text="Diamonds = " />
<Label fx:id="lblSpades" text="Spades = " />
<Label fx:id="lblClubs" text="Clubs = " />
<Label fx:id="lblFaceUp" text="Facing Up =" />
</children>
</HBox>
</children>
</AnchorPane>
What am I doing wrong, I think that solution #2 is more 'correct', but why doesn't the loader allow loading into the Card, with the StackPane cast exception
p.s. As a sub question, what difference does the root node make in this case, when loading the fxml object, will it allow me to e.g. setlayoutX property if I don't explicitly extends StackPane in the definition?
You could create a custom component that extends StackPane and is used as the controller class controller by using the <fx:root> element
public class Card extends StackPane implements Initializable {
public Card() {
FXMLLoader loader = new FXMLLoader(getClass().getResouce("card.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (Exception e) {
throw new IllegalStateException("Could not load fxml", e);
}
}
...
}
card.fxml
<fx:root type="javafx.scene.layout.StackPane"
maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fx:id="imgSuit" fitHeight="275.0" fitWidth="183.0" layoutX="10.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="imgValue" fitHeight="275.0" fitWidth="183.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../suits/back.png" />
</image>
</ImageView>
</children>
<effect>
<DropShadow />
</effect>
</fx:root>
I'm a new javaFx with Scenebuilder and MVC starter.
I have a problem as below:
I tried to make a main window to alert a new window. And I tried to midified the label text in that alert window. But I cannot do that. And I got this error:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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.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.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:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
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$149(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 48 more
Caused by: java.lang.NullPointerException
at controller.AlertController.setAlertLabel(AlertController.java:36)
at controller.MainController.onMainButtonClicked(MainController.java:24)
... 58 more
I have a main window with maincontroller, alertController and main.fxml and alert.fxml as below:
Main:
package app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("../view/Main.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Main Controller:
package controller;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class MainController {
AlertController alertController = new AlertController();
#FXML
public Label mainLabel;
#FXML
public TextField mainTextField;
#FXML
public Button mainButton;
#FXML
public void onMainButtonClicked() {
alertController.displayAlert();
alertController.setAlertLabel("Hello from MainController!");
}
}
Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainController">
<children>
<VBox prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox alignment="CENTER" prefHeight="154.0" prefWidth="600.0">
<children>
<Label fx:id="mainLabel" alignment="CENTER" contentDisplay="TOP" prefHeight="31.0" prefWidth="115.0" text="Main Text" />
</children>
</HBox>
<HBox alignment="CENTER" centerShape="false" layoutX="10.0" layoutY="10.0">
<children>
<TextField fx:id="mainTextField" alignment="TOP_LEFT" prefHeight="25.0" prefWidth="331.0" />
</children>
</HBox>
<HBox alignment="CENTER" layoutX="10.0" layoutY="10.0" prefHeight="100.0" prefWidth="200.0">
<children>
<Button fx:id="mainButton" mnemonicParsing="false" onAction="#onMainButtonClicked" text="Display Alert && Set Message Text" />
</children>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
AlertController:
package controller;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import java.io.IOException;
public class AlertController {
Stage window;
#FXML
public Label alertLabel;
#FXML
Button alertButton;
public void displayAlert() {
try {
window = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("../view/Alert.fxml"));
Scene scene = new Scene(root);
window.setScene(scene);
window.setTitle("Alert");
window.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void setAlertLabel(String str) {
alertLabel.setText(str);
}
#FXML
public void onAlertButtonClicked() {
window.close();
}
}
And Alert.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="210.0" prefWidth="380.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.AlertController">
<children>
<VBox prefHeight="210.0" prefWidth="380.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox alignment="BOTTOM_CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Label fx:id="alertLabel" text="Alert Message" />
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Button fx:id="alertButton" mnemonicParsing="false" onAction="#onAlertButtonClicked" text="Close" />
</children>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>
Please kindly help me solve this problems and explain me why this doesn't work. Thank you for you suggestions.
There are two instances of AlertController: the one you create in MainController with
AlertController alertController = new AlertController();
and the one that is created by the FXMLLoader when you load Alert.fxml in displayAlert().
The #FXML-annotated fields are initialized by the FXMLLoader on the instance it creates. However, you are calling setAlertLabel(...) on an instance of AlertController that was not created by the FXMLLoader. So when you call
alertController.setAlertLabel("Hello from MainController!");
the alertLabel belonging to alertController has not been initialized, and you get a null pointer exception.
Simple Solution
The easiest fix is to load Alert.fxml in MainController, and retrieve the correct controller instance:
public class MainController {
#FXML
public Label mainLabel;
#FXML
public TextField mainTextField;
#FXML
public Button mainButton;
#FXML
public void onMainButtonClicked() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/Alert.fxml"));
Parent root = loader.load();
AlertController alertController = loader.getController();
alertController.setAlertLabel("Hello from MainController!");
Scene scene = new Scene(root);
window.setScene(scene);
window.setTitle("Alert");
window.show();
}
}
and remove the displayAlert() method from AlertController.
Custom Component Solution
An alternative, which is structurally a bit closer to your original, is to replace the AlertController with a custom component.
Alert.fxml (note the change to the root element, and that fx:controller has been removed):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<fx:root type="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="210.0" prefWidth="380.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" >
<children>
<VBox prefHeight="210.0" prefWidth="380.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<HBox alignment="BOTTOM_CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Label fx:id="alertLabel" text="Alert Message" />
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<Button fx:id="alertButton" mnemonicParsing="false" onAction="#onAlertButtonClicked" text="Close" />
</children>
</HBox>
</children>
</VBox>
</children>
</fx:root>
AlertController.java:
package controller;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import java.io.IOException;
public class AlertController extends AnchorPane {
Stage window;
#FXML
public Label alertLabel;
#FXML
Button alertButton;
public AlertController() {
window = new Stage();
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("../view/Alert.fxml"));
loader.setController(this);
loader.setRoot(this);
loader.load();
Scene scene = new Scene(this);
window.setScene(scene);
window.setTitle("Alert");
} catch (IOException e) {
e.printStackTrace();
}
}
public void displayAlert() {
window.show();
}
public void setAlertLabel(String str) {
alertLabel.setText(str);
}
#FXML
public void onAlertButtonClicked() {
window.close();
}
}
and now your main controller can look like this:
package controller;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class MainController {
AlertController alertController = new AlertController();
#FXML
public Label mainLabel;
#FXML
public TextField mainTextField;
#FXML
public Button mainButton;
#FXML
public void onMainButtonClicked() {
alertController.displayAlert();
alertController.setAlertLabel("Hello from MainController!");
}
}
I am getting a null pointer exception while initializing a table.
This line controller.setUserDetailsList(userDetailsList) is throwing the error
even though I am adding data in userDetailsList.
Any Idea how to resolve this? Thanks..
public class Main extends Application {
private Stage primaryStage;
private ObservableList<UserDetails> userDetailsList = FXCollections.observableArrayList();
public Main() {
// Add some sample data
userDetailsList.add(new UserDetails("Hans"));
}
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
primaryStage.show();
primaryStage.setTitle("ICA");
loadMainViewPage();
}
private void loadMainViewPage(){
try{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("MainView.fxml"));
BorderPane mainView = (BorderPane) loader.load();
Scene scene = new Scene(mainView);
primaryStage.setScene(scene);
MainViewController controller = loader.getController();
controller.setUserDetailsList(userDetailsList);
}catch(Exception e) {
e.printStackTrace();
}
}
public ObservableList<UserDetails> getUserDetailsList() {
return userDetailsList;
}
public static void main(String[] args) {
launch(args);
}
}
Controller Class looks like this:
public class MainViewController {
#FXML
private TableView<UserDetails> userTable;
#FXML
private TableColumn<UserDetails,String> userNameColumn;
private Main main;
#FXML
private void initialize() {
System.out.println("inside initialize");
userNameColumn.setCellValueFactory(cellData -> cellData.getValue().HostNameProperty());
}
public void setUserDetailsList(ObservableList<UserDetails> userDetailsList) {
userTable.setItems(userDetailsList);
}
}
FXML file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainViewController">
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
<center>
<AnchorPane>
<children>
<SplitPane dividerPositions="0.362876254180602" prefHeight="375.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<TableView fx:id="userTable">
<columns>
<TableColumn fx:id="userNameColumn" prefWidth="75.0" text="C1" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
<TextArea />
</items>
</SplitPane>
</children>
</AnchorPane>
</center>
</BorderPane>
Error:
java.lang.NullPointerException
at application.Main.loadMainViewPage(Main.java:48)
at application.Main.start(Main.java:30)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I am creating a bidding system where a scene has the list of items available for auction. when the user clicks on the item, the scene should change to another anchorpane where all the details related to the item will displayed. When I click on the item I get this exception:
javafx.fxml.LoadException:
/C:/Users/Deepak95/IdeaProjects/Package2/out/production/Package2/System/view/ItemView.fxml:9
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:928)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:967)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:216)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:740)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2711)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2531)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
at System.Main.showItem(Main.java:61)
at System.View.ItemOverviewController.lambda$initialize$2(ItemOverviewController.java:23)
at System.View.ItemOverviewController$$Lambda$154/21527645.changed(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:176)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:142)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102)
at javafx.scene.control.MultipleSelectionModelBase.lambda$new$34(MultipleSelectionModelBase.java:67)
at javafx.scene.control.MultipleSelectionModelBase$$Lambda$135/26372034.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyIntegerWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:176)
at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:142)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:68)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.updateSelectedIndex(TableView.java:2900)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.select(TableView.java:2430)
at javafx.scene.control.TableView$TableViewArrayListSelectionModel.clearAndSelect(TableView.java:2360)
at javafx.scene.control.TableView$TableViewSelectionModel.clearAndSelect(TableView.java:1898)
at com.sun.javafx.scene.control.behavior.TableCellBehaviorBase.simpleSelect(TableCellBehaviorBase.java:215)
at com.sun.javafx.scene.control.behavior.TableCellBehaviorBase.doSelect(TableCellBehaviorBase.java:148)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
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.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:3758)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$255/19643353.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/10455932.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.InstantiationException: System.View.ItemViewController
at java.lang.Class.newInstance(Class.java:427)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:923)
... 72 more
Caused by: java.lang.NoSuchMethodException: System.View.ItemViewController.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 74 more
Here are the functions responsible for the scene change:
public void initRootLayout() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void showItemOverview(){
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/ItemOverview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
rootLayout.setCenter(personOverview);
ItemOverviewController controller=loader.getController();
controller.setMain(this);
} catch (IOException e) {
e.printStackTrace();
}
}
public void showItem(Item I){
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("view/ItemView.fxml"));
AnchorPane itemview= (AnchorPane) loader.load();
rootLayout.setCenter(itemview);
ItemViewController controller=loader.getController();
controller.setMain(this);
}catch(IOException e){
e.printStackTrace();
}
}
Here is the overview class and its corresponding fxml file:
package System.View;
import System.Main;
import System.Model.Item;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
public class ItemOverviewController {
#FXML
private TableView<Item> itemTableView;
#FXML
private TableColumn<Item,String> itemnamecolumn;
#FXML
private TableColumn<Item,Integer> basepricecolumn;
private Main main;
public ItemOverviewController(){
}
#FXML
private void initialize(){
itemnamecolumn.setCellValueFactory(celldata->celldata.getValue().itemnameProperty());
basepricecolumn.setCellValueFactory(celldata->celldata.getValue().initialpriceProperty().asObject());
itemTableView.getSelectionModel().selectedItemProperty().addListener((observable,oldvalue,newvalue)->main.showItem(newvalue));
}
public void setMain(Main main){
this.main=main;
itemTableView.setItems(main.getItemdata());
}
}
fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="375.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="System.View.ItemOverviewController">
<children>
<TableView fx:id="itemTableView" layoutX="200.0" layoutY="86.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columns>
<TableColumn fx:id="itemnamecolumn" prefWidth="310.0" text="Item Name" />
<TableColumn fx:id="basepricecolumn" prefWidth="289.0" text="BasePrice" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</AnchorPane>
And the itemview controller class and its fxml file:
package System.View;
import System.Model.Item;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import System.Main;
public class ItemViewController {
Item I;
#FXML
Label itemname;
#FXML
Label description;
#FXML
Label biddername;
#FXML
Label baseprice;
#FXML
Label currentprice;
Main main;
public ItemViewController(Item I){
this.I=I;
}
public void initialize(){
itemname.setText(I.getItemname());
description.setText(I.getDescription());
biddername.setText(I.getBiddername());
baseprice.setText(I.getInitialprice().toString());
currentprice.setText(I.getInitialprice().toString());
}
public void setMain(Main main) {
this.main = main;
}
}
fxml file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="System.View.ItemViewController">
<children>
<SplitPane dividerPositions="0.5" layoutX="159.0" layoutY="46.0" prefHeight="300.0" prefWidth="600.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="160.0" prefWidth="100.0">
<children>
<SplitPane dividerPositions="0.5" orientation="VERTICAL" prefHeight="298.0" prefWidth="296.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>
<ImageView fitHeight="150.0" fitWidth="296.0" layoutX="-22.0" layoutY="-2.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<GridPane prefHeight="145.0" prefWidth="294.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Base price:">
<font>
<Font size="20.0" />
</font>
</Label>
<Label text="Current Price:" GridPane.rowIndex="1">
<font>
<Font size="20.0" />
</font>
</Label>
<Label text="Bidder Name:" GridPane.rowIndex="2">
<font>
<Font size="20.0" />
</font>
</Label>
<Label fx:id="baseprice" text="Label" GridPane.columnIndex="1">
<font>
<Font size="20.0" />
</font>
</Label>
<Label fx:id="currentprice" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1">
<font>
<Font size="20.0" />
</font>
</Label>
<Label fx:id="biddername" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</GridPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Item Name:">
<font>
<Font size="20.0" />
</font>
</Label>
<Label fx:id="itemname" layoutX="134.0" layoutY="14.0" text="Label">
<font>
<Font size="20.0" />
</font>
</Label>
<Label fx:id="description" layoutX="14.0" layoutY="56.0" prefHeight="176.0" prefWidth="233.0" text="Label" />
<ButtonBar layoutX="81.0" layoutY="244.0" prefHeight="40.0" prefWidth="200.0">
<buttons>
<Button mnemonicParsing="false" text="Return" />
<Button mnemonicParsing="false" text="Bid" />
</buttons>
</ButtonBar>
</children></AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
Thanks in advance for the help :D
Edit: Inserted the packages as asked
My buttons were working well before I added the table (TableView) functionality to my code, but now an error occurs once I click on the same buttons. The error disappears and the buttons works if I comment out this lines of code in RoomsController:
intRoomIDColVR.setCellValueFactory(new PropertyValueFactory<>("roomID"));
vachRoomTypeIDColVR.setCellValueFactory(new PropertyValueFactory<>("roomTypeID"));
roomTableViewVR.setItems(olRoomData);
The FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.codetech.hotelmis.controller.RoomsController">
<children>
<BorderPane prefHeight="338.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<Accordion BorderPane.alignment="CENTER">
<panes>
<TitledPane fx:id="accVacant" animated="false" text="Vacant Rooms">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<BorderPane prefHeight="324.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<TableView fx:id="roomTableViewVR" BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="intRoomIDColVR" prefWidth="75.0" text="Room ID" />
<TableColumn fx:id="vachRoomTypeIDColVR" prefWidth="115.00000762939453" text="Room Type" />
<TableColumn fx:id="reservedColVR" prefWidth="94.0" text="Reserved?" />
<TableColumn fx:id="resCustomerColVR" prefWidth="95.0" text="Customer " />
</columns>
</TableView>
</center>
<top>
<HBox>
<children>
<Button fx:id="btnCheckInPopUp" mnemonicParsing="false" text="Check In" BorderPane.alignment="CENTER" />
</children>
</HBox>
</top>
</BorderPane>
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane fx:id="accOccupied" animated="false" text="Occupied Rooms">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<BorderPane prefHeight="324.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<HBox>
<children>
<Button fx:id="btnTabCheckOut" mnemonicParsing="false" text="Check Out" BorderPane.alignment="CENTER" />
</children>
</HBox>
</top>
<center>
<TableView BorderPane.alignment="CENTER">
<columns>
<TableColumn fx:id="intRoomIDColOR" prefWidth="75.0" text="Room ID" />
<TableColumn fx:id="vachRoomTypeIDColOR" prefWidth="115.00000762939453" text="Room Type" />
<TableColumn fx:id="occupantColOR" prefWidth="83.0" text="Occupant" />
<TableColumn fx:id="checkInDateColOR" prefWidth="121.0" text="Check In Date" />
<TableColumn fx:id="checkOutDateColOR" prefWidth="181.99996948242188" text="Expected Check Out Date" />
</columns>
</TableView>
</center>
</BorderPane>
</children>
</AnchorPane>
</content>
</TitledPane>
</panes>
</Accordion>
</center>
<top>
<HBox prefHeight="24.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="btnNewRoom" mnemonicParsing="false" text="New Room" />
<Button fx:id="btnEditRoom" mnemonicParsing="false" text="Edit Room" />
<Button fx:id="btnNewRoomType" mnemonicParsing="false" text="New Room Type" />
<Button fx:id="btnEditRoomType" mnemonicParsing="false" text="Edit Room Type" />
<Button fx:id="btnGuestLog" mnemonicParsing="false" text="Guest Log" />
</children>
</HBox>
</top>
</BorderPane>
</children>
</AnchorPane>
The Controller Class:
public class RoomsController implements Initializable {
#FXML
private TableView<RoomData> roomTableViewVR;
#FXML
private TableColumn<RoomData, Integer> intRoomIDColVR;
#FXML
private Button btnCheckInPopUp;
#FXML
private TableColumn<RoomData, String> vachRoomTypeIDColVR;
//Declare ObservableList
private ObservableList<RoomData> olRoomData;
private ActionEvent event;
private static Connection con;
private static ResultSet rs;
private static java.sql.Statement stmt;
private static String sql;
JavaToMySQL javaToMySQL = new JavaToMySQL();
public RoomsController() {
try {
con = DatabaseConnection.getConnected();
stmt = con.createStatement();
olRoomData = FXCollections.observableArrayList();
rs = con.createStatement().executeQuery("SELECT * FROM h_r_rooms");
while (rs.next()){
olRoomData.add(new RoomData(rs.getInt("intRoomID"),rs.getString("vachRoomTypeID")));
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error on Building Data");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
#Override
public void initialize(URL location, ResourceBundle resources) {
listeners();
intRoomIDColVR.setCellValueFactory(new PropertyValueFactory<>("roomID"));
vachRoomTypeIDColVR.setCellValueFactory(new PropertyValueFactory<>("roomTypeID"));
roomTableViewVR.setItems(olRoomData);
}
void listeners(){
btnCheckInPopUp.setOnAction((event -> {
System.out.println("I'm supposed to pop out a Check In window when you click me!");
Stage stage;
if (event.getSource()== btnCheckInPopUp){
stage = new Stage();
try {
Parent root = FXMLLoader.load(getClass().getResource("../view/popups/check_in.fxml"));
System.out.println("I work!");
stage.setScene(new Scene(root));
stage.setTitle("Check Customer In");
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(btnCheckInPopUp.getScene().getWindow());
stage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
}
}));
}
}
When I click on the Check In(btnCheckInPopUp) the following error appears:
The Connection is open!
javafx.fxml.LoadException:
/home/mush/Programming/Dev/InteliJProjects/Hotel_MIS/out/production/Hotel_MIS/com/codetech/hotelmis/view/popups/check_in.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2583)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at com.codetech.hotelmis.controller.RoomsController.lambda$listeners$0(RoomsController.java:87)
at com.codetech.hotelmis.controller.RoomsController$$Lambda$104/1394020985.handle(Unknown Source)
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.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:8390)
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.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:3758)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$252/507580015.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/1166661505.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at com.codetech.hotelmis.controller.RoomsController.initialize(RoomsController.java:74)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 70 more
I've really tried to figure out why but to no avail. I'd really appreciate the help and I will provide any more details concerning the code required to solve the issue.
The problem here
seems quite similar to mine.
It appears you are using the same controller class for two different FXML files. Don't do this: use a different controller for each FXML.
When the FXMLLoader's load() method is called, it does the following (somewhat simplified description):
Reads the FXML file
If the FXML file specifies a controller class, it creates a new instance of the controller class by calling its no-argument constructor
Creates an object hierarchy based on the FXML elements
Sets event handlers on the elements, mapping to the controller class instance created in step 1
For elements with an fx:id attribute, it injects the corresponding objects into any matching #FXML-annotated references in the controller instance
Calls initialize() on the controller instance, if such a method exists.
Returns the object corresponding to the root element of the FXML file.
In your case, you have two FXML files specifying the same controller class. When the "main" FXML file is loaded, the process above happens. A first instance of RoomsController is created, its #FXML-annotated fields matching fx:ids are injected (including intRoomIDColVR and others), event handlers are registered, and the initialize() method is called. All is fine at this point.
When the btnCheckInPopup button is pressed, its onAction handler is invoked. This causes check_in.fxml to be loaded. Since this specifies the same controller class, a second instance of that class is created. #FXML-annotated fields corresponding to elements of check_in.fxml with matching fx:ids are injected. However, note that, since check_in.fxml doesn't define any element with fx_id="intRoomIDColVR", intRoomIDColVR is not initialized for this second controller instance. The initialize() method is then invoked on this second controller instance, and since intRoomIDColVR is not initialized, this causes a NullPointerException at the line
intRoomIDColVR.setCellValueFactory(new PropertyValueFactory<>("roomID"));
This is a pretty good example of exactly why each FXML should have its own controller class. It gets very hard to keep track of which fields are initialized and which are not at any given time, and for any specific instance of the class, if you "share" controller classes like this.