I'm trying to make a program to Log in or register a person. But I am getting the Exception.(InvocationTargetException and the NullPointerException)
Error Stack Trace
Exception in Application start method
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/1099983479.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at gui.StartSchermController.<init>(StartSchermController.java:81)
at StartUp.start(StartUp.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/93724165.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/128893786.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/2063964656.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1108411398.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1147985808.run(Unknown Source)
... 1 more
Exception running application StartUp
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
I have problem with this line.
chTaal.setItems(FXCollections.observableArrayList(dc.keuzeTaal()));
StartSchermController.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gui;
import domein.DomeinController;
import java.io.IOException;
import java.util.ArrayList;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
/**
* FXML Controller class
*
* #author Eigenaar
*/
public class StartSchermController extends SplitPane {
#FXML
private TextField txfMeldID;
#FXML
private TextField txfMeldWw;
#FXML
private TextField txfRegNaam;
#FXML
private TextField txfRegVNaam;
#FXML
private TextField txfRegWw2;
#FXML
private TextField txfRegWw;
#FXML
private TextField txfRegID;
#FXML
private Label lblMeldAan;
#FXML
private Label lblRegistreer;
#FXML
private Label lblMeldId;
#FXML
private Label lblMeldWw;
#FXML
private Label lblRegNaam,lblRegVNaam,lblRegId,lblRegWw,lblRegWw2;
#FXML
private Button btnMeldAan,btnRegistreer;
private final DomeinController dc;
#FXML
private ComboBox cmbTaal;
#FXML
private TextField txfStartExc;
/**
* Initializes the controller class.
* #param dc
*/
public StartSchermController(DomeinController dc) {
this.dc=dc;
FXMLLoader loader=new FXMLLoader(getClass().getResource("StartScherm.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
cmbTaal.setItems(FXCollections.observableArrayList(dc.keuzeTaal()));
}
public int geefKeuze(){
int s = cmbTaal.getSelectionModel().getSelectedIndex();
return s;
}
#FXML
private void cmbTaalOnAction(ActionEvent event) {
int keuzeTaal = cmbTaal.getSelectionModel().getSelectedIndex();
System.out.println(keuzeTaal);
updateTaal(dc.setTaal(keuzeTaal));
this.geefKeuze();
}
private void updateTaal(ArrayList<String> s) {
lblMeldAan.setText(s.get(0));
this.lblMeldId.setText(s.get(1));
this.lblMeldWw.setText(s.get(2));
this.lblRegistreer.setText(s.get(3));
this.lblRegNaam.setText(s.get(4));
this.lblRegVNaam.setText(s.get(5));
this.lblRegId.setText(s.get(6));
this.lblRegWw.setText(s.get(7));
this.lblRegWw2.setText(s.get(8));
}
#FXML
private void btnMeldAanOnAction(ActionEvent event){
String id= this.txfMeldID.getText();
String ww= this.txfMeldWw.getText();
try{
dc.meldAan(id, ww);
}catch(Exception e){
txfStartExc.setText(e.getMessage());
this.txfMeldID.setText("");
this.txfMeldWw.setText("");
}
this.txfMeldID.setText("");
this.txfMeldWw.setText("");
}
#FXML
private void btnRegistreerOnAction(ActionEvent event){
String n=this.txfRegNaam.getText();
String vn=this.txfRegVNaam.getText();
String id= this.txfRegID.getText();
String ww= this.txfRegWw.getText();
String ww2=this.txfRegWw2.getText();
dc.maakSpelerAan(n, vn, id, ww);
}
}
this is the code from my fxml file generated by JavaFX Scene Builder 2.0
StartScherm.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root dividerPositions="0.17839195979899497" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" type="SplitPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.StartSchermController">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Label layoutX="352.0" text="SOKOBAN" textFill="#d7300f">
<font>
<Font name="Colonna MT" size="48.0" />
</font>
</Label>
<ComboBox fx:id="cmbTaal" layoutX="46.0" layoutY="18.0" onAction="#cmbTaalOnAction" prefWidth="150.0" />
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="257.0" prefWidth="598.0">
<children>
<SplitPane dividerPositions="0.481421647819063" layoutY="-8.0" prefHeight="400.0" prefWidth="621.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="346.0" prefWidth="295.0">
<children>
<Label fx:id="lblMeldAan" layoutX="12.0" layoutY="1.0" prefHeight="27.0" prefWidth="106.0">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="lblMeldId" layoutX="14.0" layoutY="42.0" prefHeight="17.0" prefWidth="74.0" />
<TextField fx:id="txfMeldID" layoutX="97.0" layoutY="38.0" />
<Label fx:id="lblMeldWw" layoutX="14.0" layoutY="83.0" prefHeight="17.0" prefWidth="74.0" />
<TextField fx:id="txfMeldWw" layoutX="97.0" layoutY="79.0" />
<TextField fx:id="txfStartExc" layoutX="26.0" layoutY="184.0" prefHeight="116.0" prefWidth="244.0" />
<Button fx:id="btnMeldAan" layoutX="97.0" layoutY="129.0" mnemonicParsing="false" onAction="#btnMeldAanOnAction" text="Meld aan" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label fx:id="lblRegistreer" layoutX="14.0" layoutY="1.0" prefHeight="27.0" prefWidth="106.0">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="lblRegNaam" layoutX="8.0" layoutY="41.0" prefHeight="17.0" prefWidth="113.0" text="" />
<Label fx:id="lblRegWw" layoutX="8.0" layoutY="148.0" prefHeight="17.0" prefWidth="113.0" text="" />
<Label fx:id="lblRegId" layoutX="8.0" layoutY="112.0" prefHeight="17.0" prefWidth="113.0" text="" />
<Label fx:id="lblRegVNaam" layoutX="8.0" layoutY="76.0" prefHeight="17.0" prefWidth="113.0" text="" />
<Label fx:id="lblRegWw2" layoutX="8.0" layoutY="185.0" text="" />
<TextField fx:id="txfRegNaam" layoutX="128.0" layoutY="37.0" />
<TextField fx:id="txfRegVNaam" layoutX="128.0" layoutY="72.0" />
<TextField fx:id="txfRegWw2" layoutX="128.0" layoutY="181.0" />
<TextField fx:id="txfRegWw" layoutX="128.0" layoutY="144.0" />
<TextField fx:id="txfRegID" layoutX="128.0" layoutY="108.0" />
<Button fx:id="btnRegistreer" layoutX="128.0" layoutY="226.0" mnemonicParsing="false" onAction="#btnRegistreerOnAction" text="Registreer" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children></AnchorPane>
</items>
</fx:root>
DomeinController.java
package domein;
import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;
public class DomeinController {
private final SpelerRepository spelerRepository;
private Speler speler;
// private Spel spel;
public DomeinController() {
spelerRepository = new SpelerRepository();
}
/*
public void registreer(String naam, String voornaam, String email, LocalDate geboortedatum, String wachtwoord, String wachtwoordBevestiging) {
if (!wachtwoord.equals(wachtwoordBevestiging)) {
throw new EmailException();
}*/
/*
Speler nieuweSpeler = new Speler(naam, voornaam, gebruikersID, wachtwoord);
setSpeler(nieuweSpeler); // ONTBREEKT!!
spelerRepository.voegToe(nieuweSpeler);
}*/
/** UC_1 meldAan DOING */
public void meldAan(String gebruikersID, String wachtwoord) {
Speler gevondenSpeler = spelerRepository.meldAan(gebruikersID, wachtwoord);
if (gevondenSpeler != null) {
setSpeler(gevondenSpeler);
}
}
/** UC_1 meldAan KNOWING */
public String[] geefDetailsSpeler() {
return speler.geefDetailsSpeler();
/* if (speler == null) {
return null;
}
String[] spelerS = new String[3];
spelerS[0] = speler.getVoornaam();
spelerS[1] = speler.getNaam();
return spelerS;*/
}
private void setSpeler(Speler speler){
this.speler = speler;
}
public void maakSpelerAan(String naam, String voornaam, String gebruikersID, String wachtwoord) {
Speler s=new Speler(naam,voornaam,gebruikersID,wachtwoord,false);
spelerRepository.voegToe(s);
}
public ArrayList<String> keuzeTaal(){
ArrayList<String> taal = new ArrayList<>();
taal.add("Nederlands");
taal.add("Français");
taal.add("English");
return taal;
}
public ArrayList<String> setTaal(int i){
ResourceBundle taal=setResourceBundle(i);
ArrayList<String> s= new ArrayList<>();
s.add(taal.getString("meldAan"));
s.add(taal.getString("userID"));
s.add(taal.getString("wachtwoord"));
s.add(taal.getString("registreer"));
s.add(taal.getString("naam"));
s.add(taal.getString("voornaam"));
s.add(taal.getString("userID"));
s.add(taal.getString("wachtwoord"));
s.add(taal.getString("herhalingWachtwoord"));
return s;
}
public static ResourceBundle setResourceBundle(int taalKeuze) {
Locale locale=null;
ResourceBundle taal;
if (taalKeuze==1) {
locale = Locale.FRENCH;
} else if (taalKeuze==2) {
locale = Locale.ENGLISH;
} else if(taalKeuze==0) {
locale = new Locale("nl");
}
return ResourceBundle.getBundle("taal\\LabelsBundle", locale);
}
;
}
/*
private void setSpel(Spel spel) {
this.spel = spel;
}
*/
Main Class StartUp.java
import domein.DomeinController;
import gui.StartSchermController;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.stage.Stage;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* #author Eigenaar
*/
#SuppressWarnings("unchecked")
public class StartUp extends Application
{
#Override
#SuppressWarnings("unchecked")
public void start(Stage stage)
{
DomeinController controller = new DomeinController();
StartSchermController root = new StartSchermController(controller);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Startscherm Sokoban");
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Thank you in advance. I hope somebody can help me.
I don't think your FXML is loading. Add a System.exit(1); to the catch block after you call loader.load(); to check.
If you use loader.setController(...), you should not have a fx:controller attribute in the FXML file. Remove that attribute, and it should fix the problem.
Related
I have a problem again.
I am trying to understand FXML and I am currently using the "Mastering FXML" guide of oracle "creating an address book with FXML".
I am pretty near at the end of the guide but i am not able to fix a problem I have since yesterday.
I want to write a Maintenance Program for my father but I am always getting the statement:javafx.fxml.LoadException: Maintenance is not a valid type.
there are 3 classes that are important for this problem I guess. That would be my "mainLayout.fxml", "FXMLDocumentController.java" and "Maintenance.java"
So I am just posting them in this order
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.collections.*?>
<Pane fx:controller="FXMLDocumentController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="858.0" prefWidth="1276.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<SplitPane dividerPositions="0.8687747035573122" layoutX="2.0" layoutY="25.0" prefHeight="839.0" prefWidth="1276.0">
<items>
<AnchorPane fx:id="anchorpaneLeft" minHeight="0.0" minWidth="0.0" prefHeight="811.0" prefWidth="1104.0">
<children>
<ScrollPane id="scrollpane" fx:id="scrollpane" layoutX="452.0" layoutY="319.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<TableView id="tableView" fx:id="tableView" prefHeight="834.0" prefWidth="1102.0">
<columns>
<TableColumn text="%tablecolumnName" >
<cellValueFactory><PropertyValueFactory property = "name"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnAddress" >
<cellValueFactory><PropertyValueFactory property = "address"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnNumber" >
<cellValueFactory><PropertyValueFactory property = "number"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnMail" >
<cellValueFactory><PropertyValueFactory property = "mail"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnLast" >
<cellValueFactory><PropertyValueFactory property = "last"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnNext" >
<cellValueFactory><PropertyValueFactory property = "next"/>
</cellValueFactory>
</TableColumn>
<TableColumn text="%tablecolumnNote" >
<cellValueFactory><PropertyValueFactory property = "note"/>
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
<Maintenance name="jacob" address="fichtestraße 5" number="01515242" mail="jacob#jacob.com" last="10.10.2018" next="10.10.2019" note="leer jung"/>
</FXCollections>
</items>
</TableView>
</content>
</ScrollPane>
</children>
</AnchorPane>
<AnchorPane fx:id="anchorpaneRight" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<VBox fx:id="vboxNew" alignment="TOP_CENTER" layoutX="50.0" layoutY="52.0" prefHeight="85.0" prefWidth="164.0" spacing="15.0" AnchorPane.bottomAnchor="726.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="26.0">
<children>
<Label fx:id="labelNew" prefHeight="17.0" prefWidth="192.0" text="%labelNew">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Button fx:id="buttonInsert" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonInsert" onAction="#handleInsertButton" textAlignment="CENTER" />
</children>
</VBox>
<VBox fx:id="vboxEdit" alignment="TOP_CENTER" layoutY="120.0" prefHeight="717.0" prefWidth="164.0" spacing="15.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="120.0">
<children>
<Label fx:id="labelEdit" prefHeight="17.0" prefWidth="166.0" text="%labelEdit">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Button fx:id="buttonEdit" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonEdit" textAlignment="CENTER" />
<Button fx:id="buttonDelete" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonDelete" />
<Button fx:id="buttonWeek" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonWeek" />
<Button fx:id="buttonMonth" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonMonth" />
<Button fx:id="buttonYear" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="%buttonYear" />
</children>
</VBox>
</children>
</AnchorPane>
</items>
</SplitPane>
<MenuBar fx:id="menubar" layoutX="2.0" layoutY="2.0" prefHeight="25.0" prefWidth="1276.0">
<menus>
<Menu fx:id="menuFile" mnemonicParsing="false" text="%menuFile">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu fx:id="menuEdit" mnemonicParsing="false" text="%menuEdit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu fx:id="menuHelp" mnemonicParsing="false" text="%menuHelp">
<items>
<MenuItem fx:id="itemAbout" mnemonicParsing="false" text="%itemAbout" />
</items>
</Menu>
<Menu fx:id="menuLanguage" mnemonicParsing="false" text="%menuLanguage">
<items>
<MenuItem fx:id="itemGer" mnemonicParsing="false" text="%itemGer" />
<MenuItem fx:id="itemEng" mnemonicParsing="false" text="%itemEng" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</Pane>
FXMLDocumentController.java:
import java.net.URL;
import java.util.*;
import javafx.collections.*;
import javafx.event.*;
import javafx.fxml.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.*;
public class FXMLDocumentController implements Initializable {
#FXML private Label label;
#FXML private TableView<Maintenance> table;
#FXML private TableColumn tablecolumnName;
#FXML private TableColumn tablecolumnAddress;
#FXML private TableColumn tablecolumnNumber;
#FXML private TableColumn tablecolumnMail;
#FXML private TableColumn tablecolumnLast;
#FXML private TableColumn tablecolumnNext;
#FXML private TableColumn tablecolumnNote;
#FXML private Button buttonInsert;
#FXML private Button buttonEdit;
#FXML private Button buttonDelete;
#FXML private Button buttonWeek;
#FXML private Button buttonMonth;
#FXML private Button buttonYear;
#FXML private TextField textfieldName;
#FXML private TextField textfieldAddress;
#FXML private TextField textfieldMail;
#FXML private TextField textfieldNumber;
#FXML private TextField textfieldLast;
#FXML private TextField textfieldNext;
#FXML private TextField textfieldNote;
#FXML private TableView<Maintenance> tableView;
#FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
#FXML
void handleInsertButton(ActionEvent event){
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("insertWindow.fxml"));
Parent root1 = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle("Insert Window");
Scene scene = new Scene (root1);
stage.setScene(scene);
stage.show();
} catch(Exception e){
e.printStackTrace();
}
}
protected void handleEditButton() {
}
protected void handleDeleteButton() {
}
protected void handleWeekButton() {
}
protected void handleMonthButton() {
}
protected void handleYearButton() {
}
protected void handleGerButton() {
}
protected void handleEngButton() {
}
#FXML
protected void addMaintenance(ActionEvent event){
ObservableList <Maintenance> data = tableView.getItems();
data.add(new Maintenance(textfieldName.getText(), textfieldAddress.getText(),
textfieldMail.getText(), textfieldNumber.getText(),
textfieldLast.getText(), textfieldNext.getText(),
textfieldNote.getText()));
textfieldName.setText("");
textfieldAddress.setText("");
textfieldMail.setText("");
textfieldNumber.setText("");
textfieldLast.setText("");
textfieldNext.setText("");
textfieldNote.setText("");
}
}
and finally Maintenance.java
import javafx.beans.property.SimpleStringProperty;
public class Maintenance {
private final SimpleStringProperty name = new SimpleStringProperty("");
private final SimpleStringProperty address = new SimpleStringProperty("");
private final SimpleStringProperty number = new SimpleStringProperty("");
private final SimpleStringProperty mail = new SimpleStringProperty("");
private final SimpleStringProperty last = new SimpleStringProperty("");
private final SimpleStringProperty next = new SimpleStringProperty("");
private final SimpleStringProperty note = new SimpleStringProperty("");
public Maintenance(){
this("", "", "", "", "", "", "");
}
public Maintenance(String name, String address, String number, String mail, String last, String next, String note){
setName(name);
setAddress(address);
setNumber(number);
setMail(mail);
setLast(last);
setNext(next);
setNote(note);
}
public String getName() {
return name.get();
}
public String getAddress() {
return address.get();
}
public String getNumber() {
return number.get();
}
public String getMail() {
return mail.get();
}
public String getLast() {
return last.get();
}
public String getNext() {
return next.get();
}
public String getNote() {
return note.get();
}
public final void setName(String Name){
name.set(Name);
}
public final void setAddress(String Address){
address.set(Address);
}
public final void setNumber(String Number){
number.set(Number);
}
public final void setMail(String Mail){
mail.set(Mail);
}
public final void setLast(String Last){
last.set(Last);
}
public final void setNext(String Next){
next.set(Next);
}
public final void setNote(String Note){
note.set(Note);
}
}
insertwindow.fxml is existing and worked great so far so I won't post it to shorten my post same for main.java and my 4 property files.
Hope someone can tell me what I am doing wrong again.
Thanks for help!
Executing C:\Users\Schurke\Documents\NetBeansProjects\Maintenance\dist\run365389303\Maintenance.jar using platform D:\Program Files\Java\jdk1.8.0_202\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 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 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
file:/C:/Users/Schurke/Documents/NetBeansProjects/Maintenance/dist/run365389303/Maintenance.jar!/mainLayout.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3116)
at Main.start(Main.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
... 1 more
Caused by: java.lang.ClassNotFoundException
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2899)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
... 19 more
Exception running application Main
Java Result: 1
For custom FXML type you should add an import statement:
<?import Maintenance?>
In general it is bad practice to use default packages. Put the Maintenance.java into some package and import it as:
<?import yourPackageName.Maintenance?>
This is what my view looks like:
Explanation
The entire window itself runs on one controller, called the CartWindowController, and the product list itself is a JavaFX Custom Control called CartItemComponent, which has it's own controller. Each item in the list, therefore has it's own instance of the controller as I programmatically make an instance and populate a VBox.
Problem
I cannot figure out how to notify the CartWindowController when the user clicks on the "X" button, which is handled by the "CartItemComponent" Controller. I would highly appreciate it if anyone could give me a heads up on how to tackle this problem.
Here's what my FXML looks like for the entire Window:
CartWindow.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<AnchorPane id="AnchorPane" fx:id="parent" prefHeight="400.0" prefWidth="600.0" styleClass="pane" stylesheets="#../assets/userwindow.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ordermanagementsystem.controllers.CartWindowController">
<children>
<Button layoutX="25.0" layoutY="25.0" mnemonicParsing="false" onAction="#exitWindow" prefHeight="35.0" prefWidth="20.0" styleClass="back-button" />
<Label layoutX="262.0" layoutY="17.0" styleClass="heading" text="Cart" />
<ScrollPane hbarPolicy="NEVER" layoutY="97.0" prefHeight="315.0" prefWidth="600.0" styleClass="no-padding">
<content>
<AnchorPane prefWidth="600.0" styleClass="no-padding">
<children>
<VBox fx:id="productList" layoutX="25.0" prefWidth="350.0" />
<AnchorPane layoutX="425.0" layoutY="25.0" prefWidth="150.0" styleClass="no-padding">
<children>
<Label layoutX="18.0" styleClass="heading-sub" text="Summary" />
<Label layoutX="1.0" layoutY="55.0" text="Gross:" />
<Label fx:id="grossTotal" alignment="CENTER_RIGHT" layoutX="47.0" layoutY="55.0" prefHeight="17.0" prefWidth="103.0" text="RM0.00" />
<Label layoutX="1.0" layoutY="75.0" text="Packaging:" />
<Label fx:id="packagingTotal" alignment="CENTER_RIGHT" layoutX="73.0" layoutY="75.0" prefHeight="17.0" prefWidth="77.0" text="RM0.00" />
<Label layoutX="1.0" layoutY="95.0" text="Total:" />
<Label fx:id="total" alignment="CENTER_RIGHT" layoutX="40.0" layoutY="95.0" prefHeight="17.0" prefWidth="110.0" styleClass="green-text" text="RM0.00" />
<Button layoutY="125.0" mnemonicParsing="false" onAction="#checkout" prefHeight="39.0" prefWidth="150.0" styleClass="action-button" text="Check Out" />
</children>
</AnchorPane>
</children>
</AnchorPane>
</content>
</ScrollPane>
</children>
</AnchorPane>
CartWindowController.java
package ordermanagementsystem.controllers;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import ordermanagementsystem.cart.CartState;
import ordermanagementsystem.orders.models.OrderItem;
import ordermanagementsystem.viewcomponents.CartItemComponent;
public class CartWindowController extends ViewController implements Initializable {
#FXML
public Parent parent;
#FXML
private VBox productList;
#FXML
private Label grossTotal;
#FXML
private Label packagingTotal;
#FXML
private Label total;
private CartState cartState;
#FXML
private void exitWindow(ActionEvent event) {
try {
this.openPage(this.parent, "MainWindow.fxml");
} catch (IOException ex) {
ex.printStackTrace();
}
}
#FXML
private void checkout(ActionEvent event) {
}
#Override
public void initialize(URL url, ResourceBundle rb) {
this.cartState = CartState.getInstance();
for (OrderItem item : this.cartState.getItems()) {
this.productList.getChildren().add(new CartItemComponent(item));
}
}
}
CartItemComponent.java:
package ordermanagementsystem.viewcomponents;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import ordermanagementsystem.DialogBox;
import ordermanagementsystem.cart.CartState;
import ordermanagementsystem.orders.models.OrderItem;
public class CartItemComponent extends AnchorPane {
#FXML
private AnchorPane frame;
#FXML
private TextField quantity;
#FXML
private Label total;
#FXML
private Label productName;
private OrderItem item;
private CartState cartState;
public CartItemComponent(OrderItem item) {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/ordermanagementsystem/views/components/CartItemComponent.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
exception.getStackTrace();
}
this.cartState = CartState.getInstance();
this.item = item;
this.setQuantity(1);
this.quantity.setEditable(false);
this.productName.setText(this.item.getProduct().getName());
}
private void setQuantity(int quantity) {
this.quantity.setText(String.valueOf(quantity));
this.item.setQuantity(quantity);
this.cartState.updateItem(this.item);
this.total.setText("RM" + String.format("%.2f", item.getProduct().getPrice() * quantity));
}
private int getQuantity() {
return Integer.parseInt(this.quantity.getText());
}
private void updateSummary() {
}
#FXML
private void add(ActionEvent event) {
int quantity = this.getQuantity();
if (quantity == 99) {
DialogBox.showValidationDialog("The quantity cannot be over 99.");
} else {
this.setQuantity(quantity + 1);
}
}
#FXML
private void substract(ActionEvent event) {
int quantity = this.getQuantity();
if (quantity == 1) {
DialogBox.showValidationDialog("The quantity cannot be below 1.");
} else {
this.setQuantity(quantity - 1);
}
}
}
Something you could try is to pass the CartWindowController into the CartItemComponent constructor, then when you need to notify the CartWindowController, you call a method or set a flag or trigger an event, your choice! All you would need to do is add a parameter of type CartWindowController to you CartItemComponent and just save the reference.
I am currently writing a poker game as an assignment for my finals. I have to use a combined view: a class with actual code and the Scenebuilder. I did everything i learned and the model is working just fine. I'm new to this so it must be an easy fix! The professors want us to get familiar with Scenebuilder but they didn't give much of an explanation.
As you can see i use a class called pokerView in which I load in every single image of the cards. In the beginning I use a strange forloop to automatically generate the names of the images, I printed it out and the url is okay!
I also use the scenebuilder. It has it's own controller/view class that is the FXMLpokerController
And to combine those two, I used a "super" view.
Funny thing is that java does not give me any error!
I really really need an answer. If the window show up empty I cannot show the app.
So the terminal gives me the following lines when using the printstacktrace
javafx.fxml.LoadException:
file:/Users/test/Documents/2%20aba/Informatica/Netbeans%20projecten/poker/dist/run1845271650/poker.jar!/poker/FXMLpoker.fxml:10
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at poker.ViewPlus.<init>(ViewPlus.java:30)
at poker.MAINpoker.start(MAINpoker.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.Button field poker.FXMLpokerController.deal to javafx.scene.layout.AnchorPane
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 10 more
I don't know what this means
this is the pokerview
package poker;
import javafx.scene.layout.Region;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import java.util.ArrayList;
import java.net.URL;
public class PokerView extends Region{
private Game model;
private Image image;
private ImageView imv;
private ArrayList<ImageView> stapelView = new ArrayList<>();
public PokerView(Game model){
this.model=model;
}
public void configureerStapel(){
for(Kaart k:model.getRound().getStapel().getList()){
URL imageURL = getClass().getResource("/res/"+k.toString());
System.out.println(""+imageURL);
imv = new ImageView(imageURL.toExternalForm());
stapelView.add(imv);
}
}
public void turnHandCards(){
int index1 = model.getRound().wieIsAanDeBeurt().getHand().get(0).getIndex();
ImageView k = stapelView.get(index1);
System.out.println("de eerste hand card in de view ="+index1);
k.setLayoutX(166);
k.setLayoutY(200);
k.setFitHeight(141);
k.setFitWidth(95);
int index2 = model.getRound().wieIsAanDeBeurt().getHand().get(1).getIndex();
ImageView s = stapelView.get(index2);
s.setLayoutX(297);
s.setLayoutY(200);
s.setFitHeight(141);
s.setFitWidth(95);
getChildren().addAll(k, s);
}
public void turnThreeTableCards(){
int index1 = model.getRound().getTafel().getFirstCard().getIndex();
ImageView i = stapelView.get(index1);
i.setLayoutX(447);
i.setLayoutY(24);
i.setFitHeight(141);
i.setFitWidth(95);
int index2 = model.getRound().getTafel().getSecondCard().getIndex();
ImageView j = stapelView.get(index2);
j.setLayoutX(340);
j.setLayoutY(24);
j.setFitHeight(141);
j.setFitWidth(95);
int index3 = model.getRound().getTafel().getThirdCard().getIndex();
ImageView k = stapelView.get(index3);
k.setLayoutX(231);
k.setLayoutY(24);
k.setFitHeight(141);
k.setFitWidth(95);
getChildren().addAll(i, j, k);
}
public void turnLastTwoTableCards(){
int index1 = model.getRound().getTafel().getFourthCard().getIndex();
ImageView i = stapelView.get(index1);
i.setLayoutX(123);
i.setLayoutY(24);
i.setFitHeight(141);
i.setFitWidth(95);
int index2 = model.getRound().getTafel().getFifthCard().getIndex();
ImageView j = stapelView.get(index2);
j.setLayoutX(14);
j.setLayoutY(24);
j.setFitHeight(141);
j.setFitWidth(95);
getChildren().addAll(i, j);
}
}
this is the FXMLpokerController
package poker;
import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import java.lang.*;
public class FXMLpokerController {
#FXML
private Button raise;
#FXML
private Button check;
#FXML
private Button fold;
#FXML
private Button ready;
#FXML
private TextField stake;
#FXML
private TextArea creditsLeft;
#FXML
private TextArea pot;
#FXML
private TextArea minimumStake;
#FXML
private TextArea isAanDeBeurt;
#FXML
private Button deal;
#FXML
void initialize(){
raise.setOnAction(this::raise);
check.setOnAction(this::check);
fold.setOnAction(this::fold);
ready.setOnAction(this::ready);
deal.setOnAction(this::deal);
pot.setText(""+model.getPot());
creditsLeft.setText(""+model.getRound().wieIsAanDeBeurt().getCredit());
}
private Game model;
private ViewPlus view;
private int counter;
public void raise(ActionEvent e){
int cred = Integer.parseInt(stake.getText());
model.getRound().wieIsAanDeBeurt().setStake(cred);
model.getRound().wieIsAanDeBeurt().setCredit(model.getRound().wieIsAanDeBeurt().getCredit()-cred);
model.getRound().getTafel().setPot(model.getRound().getTafel().getPot()+cred);
model.getRound().volgendeSpeler();
minimumStake.setText(""+cred);
automaticFold();
}
public void check(ActionEvent e){
model.getRound().volgendeSpeler();
automaticFold();
}
public void fold(ActionEvent e){
model.getRound().wieIsAanDeBeurt().doetNietMeerMee(true);
automaticFold();
}
public void setModel(Game model){
this.model=model;
model = new Game();
}
public void setView(ViewPlus view){
this.view=view;
}
public void automaticFold(){
int cred = Integer.parseInt(minimumStake.getText());
if(model.getRound().wieIsAanDeBeurt().getCredit()<cred){
model.getRound().wieIsAanDeBeurt().doetNietMeerMee(true);
}
}
public void ready(ActionEvent e){
model.getRound().volgendeSpeler();
}
public void setName(){
isAanDeBeurt.setText(model.getRound().wieIsAanDeBeurt().getName());
String name = isAanDeBeurt.getText();
model.getRound().wieIsAanDeBeurt().setName(name);
}
public void deal(ActionEvent e){
model.startOver();
view.configureerKaarten();
view.turnHandCards();
counter++;
}
/**
* #return the counter
*/
public int getCounter() {
return counter;
}
}
this is the superview called viewplus
package poker;
import javafx.scene.Parent;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;
public class ViewPlus extends AnchorPane {
private Game model;
private PokerView view;
private Parent fxmlView;
private FXMLpokerController fxmlController;
public ViewPlus(Game model) throws IOException{
this.model = model;
view = new PokerView(model);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("FXMLpoker.fxml"));
try{
fxmlView = loader.load();
fxmlController = loader.getController();
fxmlController.setModel(model);
fxmlController.setView(this);
getChildren().addAll(fxmlView,view);
} catch(IOException ex){ex.printStackTrace();}
}
public void configureerKaarten(){
view.configureerStapel();
}
public void turnHandCards(){
view.turnHandCards();
}
public void turnThreeTableCards(){
view.turnThreeTableCards();
}
}
this is the View controller
package poker;
public class Controller {
private Game model;
private ViewPlus view;
public Controller(Game model, ViewPlus view){
this.model=model;
this.view=view;
view.configureerKaarten();
view.turnHandCards();
if(model.getRound().getSpeler1().getStake() == model.getRound().getSpeler2().getStake()
&& model.getRound().getSpeler2().getStake() == model.getRound().getSpeler3().getStake()
&& model.getRound().getSpeler3().getStake() == model.getRound().getSpeler4().getStake()){
view.turnThreeTableCards();
}
}
}
this is my main class
package poker;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MAINpoker extends Application {
#Override
public void start(Stage stage) throws Exception {
Game model = new Game();
ViewPlus view = new ViewPlus (model);
Scene scene = new Scene(view);
stage.setScene(scene);
stage.show();
Controller c = new Controller(model, view);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
this is my FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.image.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" fx:id="deal" prefHeight="605.0" prefWidth="556.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="poker.FXMLpokerController">
<children>
<Button layoutX="484.0" layoutY="556.0" mnemonicParsing="false" text="ready" />
<Button layoutX="249.0" layoutY="495.0" mnemonicParsing="false" text="deal" />
<Label layoutX="393.0" layoutY="495.0" prefHeight="42.0" prefWidth="160.0" text="minimum stake">
<graphic>
<TextArea fx:id="minimumStake" prefHeight="28.0" prefWidth="28.0" />
</graphic>
</Label>
<Button fx:id="raise" layoutX="14.0" layoutY="405.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="69.0" text="raise" />
<Button fx:id="check" layoutX="251.0" layoutY="405.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="69.0" text="check" />
<Button fx:id="fold" layoutX="473.0" layoutY="405.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="69.0" text="fold" />
<Label layoutX="17.0" layoutY="548.0" prefHeight="42.0" prefWidth="148.0" text="name">
<graphic>
<TextArea fx:id="isAanDeBeurt" prefHeight="42.0" prefWidth="97.0" />
</graphic>
</Label>
<Label layoutX="17.0" layoutY="495.0" text="pot">
<graphic>
<TextArea fx:id="pot" prefHeight="36.0" prefWidth="47.0" />
</graphic>
</Label>
<Label fx:id="label" layoutX="409.0" layoutY="450.0" minHeight="16" minWidth="69" text="stake">
<graphic>
<TextField prefHeight="20.0" prefWidth="91.0" />
</graphic></Label>
<ImageView fx:id="tafel" fitHeight="391.0" fitWidth="556.0">
<image>
<Image url="#../afbeelding/dark_green_colour_background.jpg" />
</image>
</ImageView>
<Pane layoutX="14.0" layoutY="24.0">
<children>
<ImageView fx:id="tafel1" fitHeight="141.0" fitWidth="95.0">
<image>
<Image url="#../afbeelding/back.jpg" />
</image>
</ImageView>
</children>
</Pane>
<Label fx:id="label1" layoutX="17.0" layoutY="450.0" minHeight="16" minWidth="69" prefHeight="16.0" prefWidth="119.0" text="credits left">
<graphic>
<TextArea fx:id="creditsLeft" prefHeight="16.0" prefWidth="37.0" />
</graphic>
</Label>
<Pane layoutX="142.0" layoutY="246.0" />
<Pane layoutX="123.0" layoutY="24.0">
<children>
<ImageView fx:id="tafel2" fitHeight="141.0" fitWidth="95.0">
<image>
<Image url="#../afbeelding/back.jpg" />
</image>
</ImageView>
</children>
</Pane>
<Pane layoutX="231.0" layoutY="24.0">
<children>
<ImageView fx:id="tafel3" fitHeight="141.0" fitWidth="95.0">
<image>
<Image url="#../afbeelding/back.jpg" />
</image>
</ImageView>
</children>
</Pane>
<Pane layoutX="340.0" layoutY="24.0">
<children>
<ImageView fx:id="tafel4" fitHeight="141.0" fitWidth="95.0">
<image>
<Image url="#../afbeelding/back.jpg" />
</image>
</ImageView>
</children>
</Pane>
<Pane layoutX="447.0" layoutY="24.0">
<children>
<ImageView fx:id="tafel5" fitHeight="141.0" fitWidth="95.0">
<image>
<Image url="#../afbeelding/back.jpg" />
</image>
</ImageView>
</children>
</Pane>
<Pane layoutX="166.0" layoutY="200.0">
<children>
<ImageView fx:id="hand1" fitHeight="141.0" fitWidth="95.0">
<image>
<Image url="#../afbeelding/back.jpg" />
</image>
</ImageView>
</children>
</Pane>
<Pane layoutX="297.0" layoutY="200.0">
<children>
<ImageView fx:id="hand2" fitHeight="141.0" fitWidth="95.0">
<image>
<Image url="#../afbeelding/back.jpg" />
</image>
</ImageView>
</children>
</Pane>
</children>
You have named your AnchorPane "deal" in the FXML and your Button as "deal" in the controller. During initialization, the mapping is done by name. Since these objects are not the same type, you are getting a class exception.
If the button is really deal, move the id from the AnchorPane to the button in the FXML. Based on this:
deal.setOnAction(this::deal);
I assume the button should be called deal.
Change this line
<AnchorPane id="AnchorPane" prefHeight="605.0" prefWidth="556.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="poker.FXMLpokerController">
and this line
<Button fx:id="deal" layoutX="249.0" layoutY="495.0" mnemonicParsing="false" text="deal" />
Your AnchorPane has the fx:id "deal":
<AnchorPane id="AnchorPane" fx:id="deal" prefHeight="605.0" prefWidth="556.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="poker.FXMLpokerController">
In your controller class FXMLpokerController you define "deal" as a button:
#FXML
private Button deal;
The FXML injection cannot initialize a Button as an AnchorPane. Move the fx:id to the correct button and give your AnchorPane a different fx:id.
I am unable to populate my tableview. I believe the problem is in the controller, in the way my data is being sent to the FXML file, because a system out print (see below) shows exactly what I have in my database.
Please let me know where I did a mistake. I went over all tutorials that exist on that, but nothing fits my problem.
Thanks
Main App:
package tableview;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainApp extends Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("view/FXMLTable.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Controller:
import tableview.model.Person;
public class FXMLTableController{
#FXML
public TableView<Person> tableview ;
#FXML
private TableColumn<Person, Number> clientIdColumn;
#FXML
private TableColumn<Person, String> firstNameColumn;
#FXML
private TableColumn<Person, String> lastNameColumn;
#FXML
private void initialize() {
assert tableview != null : "fx:id=\"tableview\" was not injected: check your FXML file 'UserMaster.fxml'.";
clientIdColumn.setCellValueFactory(cellData -> cellData.getValue().
clientIDProperty());
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue()
.firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue()
.lastNameProperty());
buildData();
}
private ObservableList<Person> data;
public void buildData(){
data = FXCollections.observableArrayList();
Connection con = null;
try {
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:tableviewdb.db");
String SQL = "Select * from INFO";
ResultSet rs = con.createStatement().executeQuery(SQL);
while(rs.next()){
Person per = new Person();
per.ClientID.set(rs.getInt("CLIENTID"));
per.FirstName.set(rs.getString("FIRSTNAME"));
per.LastName.set(rs.getString("LASTNAME"));
data.add(per);
}
tableview = new TableView<Person>();
tableview.setItems(data);
System.out.println(tableview.getItems().get(1).ClientID);
}
catch(Exception e){
e.printStackTrace();
System.out.println("Error on Building Data");
}
}
}
Model Class:
package tableview.model;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Person {
public SimpleIntegerProperty ClientID = new SimpleIntegerProperty();
public SimpleStringProperty FirstName = new SimpleStringProperty();
public SimpleStringProperty LastName = new SimpleStringProperty();
public SimpleIntegerProperty getClientID() {
return ClientID;
}
public SimpleStringProperty getFirstname() {
return FirstName;
}
public SimpleStringProperty getLastName() {
return LastName;
}
public IntegerProperty clientIDProperty(){
return ClientID;
}
public StringProperty firstNameProperty(){
return FirstName;
}
public StringProperty lastNameProperty(){
return LastName;
}
}
FXML file:
(disregard the save button for now...)
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<children>
<SplitPane prefHeight="400.0" prefWidth="600.0">
<items>
<SplitPane dividerPositions="0.5" orientation="VERTICAL" prefHeight="200.0" prefWidth="160.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TextField layoutX="93.0" layoutY="34.0" />
<TextField layoutX="93.0" layoutY="85.0" />
<Label layoutX="35.0" layoutY="39.0" text="name" />
<Label layoutX="35.0" layoutY="90.0" text="email" />
<Button layoutX="204.0" layoutY="140.0" mnemonicParsing="false" text="save" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TableView layoutY="-2.0" prefHeight="200.0" prefWidth="598.0">
<columns>
<TableColumn prefWidth="302.0" text="name" />
<TableColumn prefWidth="295.0" text="email" />
</columns>
</TableView>
</children>
</AnchorPane>
</items>
</SplitPane>
</items>
</SplitPane>
</children>
</AnchorPane>
You're creating a new TableView and setting its items, instead of setting the items on the table that the FXML file defined. Remove the
tableView = new TableView<Person>();
from the controller.
To get the #FXML-annotated fields in the controller to be populated with the appropriate elements from the FXML file, you need to add fx:id attributes to those elements:
<TableView fx:id="tableview" ... >
<columns>
<TableColumn fx:id="firstNameColumn" ... />
...
I'm trying to make a program to Log in or register a person.
But I keep getting the same Exception.(InvocationTargetException)
Exception in Application start method
file:/C:/Users/Eigenaar/Desktop/p1g04/dist/SokobanGroep4.jar!/gui/StartScherm.fxml:19
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/1099983479.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at gui.StartSchermController.<init>(StartSchermController.java:73)
at StartUp.start(StartUp.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/2006606717.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/128893786.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/2063964656.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1108411398.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1147985808.run(Unknown Source)
... 1 more
Exception running application StartUp
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
This is the code from my StartSchermController(the controller from my fxml file)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gui;
import domein.DomeinController;
import java.io.IOException;
import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
/**
* FXML Controller class
*
* #author Eigenaar
*/
public class StartSchermController extends SplitPane {
#FXML
private ChoiceBox chbTaal;
#FXML
private TextField txfMeldID;
#FXML
private TextField txfMeldWw;
#FXML
private TextField txfRegNaam;
#FXML
private TextField txfRegVNaam;
#FXML
private TextField txfRegWw2;
#FXML
private TextField txfRegWw;
#FXML
private TextField txfRegID;
#FXML
private Label lblMeldAan;
#FXML
private Label lblRegistreer;
#FXML
private Label lblMeldId;
#FXML
private Label lblMeldWw;
#FXML
private Label lblRegNaam,lblRegVNaam,lblRegId,lblRegWw,lblRegWw2;
private final DomeinController dc;
/**
* Initializes the controller class.
*/
public StartSchermController(DomeinController dc) {
this.dc=dc;
FXMLLoader loader=new FXMLLoader(getClass().getResource("StartScherm.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
try{
chbTaal.setItems(FXCollections.observableArrayList(dc.keuzeTaal()));
} catch(NullPointerException e){
System.out.println(e.getMessage());
}
}
public int geefKeuze(){
int s = chbTaal.getSelectionModel().getSelectedIndex();
return s;
}
#FXML
private void actionchbTaal(ActionEvent event) {
int keuzeTaal = chbTaal.getSelectionModel().getSelectedIndex();
update(dc.setTaal(keuzeTaal));
this.geefKeuze();
}
private void update(ArrayList<String> s) {
lblMeldAan.setText(s.get(0));
this.lblMeldId.setText(s.get(1));
this.lblMeldWw.setText(s.get(2));
this.lblRegistreer.setText(s.get(3));
this.lblRegNaam.setText(s.get(4));
this.lblRegVNaam.setText(s.get(5));
this.lblRegId.setText(s.get(6));
this.lblRegWw.setText(s.get(7));
this.lblRegWw2.setText(s.get(8));
}
}
this is the code from my fxml file generated by JavaFX Scene Builder 2.0
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root dividerPositions="0.19095477386934673" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" type="SplitPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="148.0" prefWidth="598.0">
<children>
<Label layoutX="384.0" layoutY="21.0" text="SOKOBAN" textFill="#d7300f">
<font>
<Font name="Colonna MT" size="24.0" />
</font>
</Label>
<ComboBox fx:id="chbTaal" layoutX="37.0" layoutY="24.0" onAction="#actionchbTaal" prefWidth="150.0" />
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<SplitPane dividerPositions="0.481421647819063" layoutY="-6.0" prefHeight="331.0" prefWidth="621.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label fx:id="lblMeldAan" layoutX="12.0" layoutY="1.0" prefHeight="27.0" prefWidth="106.0">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="lblMeldId" layoutX="14.0" layoutY="42.0" prefHeight="17.0" prefWidth="74.0" />
<TextField fx:id="txfMeldID" layoutX="97.0" layoutY="38.0" />
<Label fx:id="lblMeldWw" layoutX="14.0" layoutY="83.0" prefHeight="17.0" prefWidth="74.0" />
<TextField fx:id="txfMeldWw" layoutX="97.0" layoutY="79.0" />
<TextField layoutX="30.0" layoutY="165.0" prefHeight="116.0" prefWidth="244.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<Label fx:id="lblRegistreer" layoutX="14.0" layoutY="1.0" prefHeight="27.0" prefWidth="106.0">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="lblRegNaam" layoutX="82.0" layoutY="41.0" text="Naam:" />
<Label fx:id="lblRegWw" layoutX="51.0" layoutY="148.0" text="Wachtwoord:" />
<Label fx:id="lblRegId" layoutX="51.0" layoutY="112.0" text="GebruikersID:" />
<Label fx:id="lblRegVNaam" layoutX="58.0" layoutY="76.0" text="Voornaam:" />
<Label fx:id="lblRegWw2" layoutX="8.0" layoutY="185.0" text="Controle Wachtwoord:" />
<TextField fx:id="txfRegNaam" layoutX="128.0" layoutY="37.0" />
<TextField fx:id="txfRegVNaam" layoutX="128.0" layoutY="72.0" />
<TextField fx:id="txfRegWw2" layoutX="128.0" layoutY="181.0" />
<TextField fx:id="txfRegWw" layoutX="128.0" layoutY="144.0" />
<TextField fx:id="txfRegID" layoutX="128.0" layoutY="108.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children></AnchorPane>
</items>
</fx:root>
And this is the code from my domeinController where the code to set the language set.(The choice for a language is done with resourcebundels)
import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;
public class DomeinController {
public ArrayList<String> keuzeTaal(){
ArrayList<String> taal = new ArrayList<>();
taal.add("Nederlands");
taal.add("Français");
taal.add("English");
return taal;
}
public ArrayList<String> setTaal(int i){
ResourceBundle taal=Taal(i);
ArrayList<String> s= new ArrayList<>();
s.add(taal.getString("meldAan"));
s.add(taal.getString("gebruikersID"));
s.add(taal.getString("wachtwoord"));
s.add(taal.getString("registreer"));
s.add(taal.getString("naam"));
s.add(taal.getString("voornaam"));
s.add(taal.getString("gebruikersID"));
s.add(taal.getString("wachtwoord"));
s.add(taal.getString("herhalingWachtwoord"));
return s;
}
public static ResourceBundle Taal(int taalKeuze) {
Locale locale;
ResourceBundle taal;
if (taalKeuze==1) {
locale = Locale.FRENCH;
} else if (taalKeuze==2) {
locale = Locale.ENGLISH;
} else {
locale = new Locale("nl");
}
return ResourceBundle.getBundle("taal\\LabelsBundle", locale);
}
;
}
Thank you in advance. I hope somebody can help me. (sorry for my bad english)
Your controller is declaring chTaal as a ChoiceBox but your FXML is declaring it as a ComboBox. The FXMLLoader can't coerce a conversion between those types so it doesn't set chTaal to anything, leaving it as null. Make the types match in the two files and see if that corrects your issue.