Exception in Application start method java with fxml - javafx

So i made pretty simple application with Fxml with only label and text field, and two buttons but i get this error..
Exception in Application start method
And here is code
<?xml version="1.0" encoding="UTF-8*?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<GridPane alignment="CENTER" hgap="5.0" vgap="5.0">
<children>
<Text text="Dodavanje Kupca" GridPane.columnIndex="0" GridPane.rowIndex="0" GridPane.columnSpan="2">
<font>
<Font size= "30.0"/>
</font>
</Text>
<Label text="Ime Kupca" GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<Label text="Prezime Kupca" GridPane.columnIndex="0" GridPane.rowIndex="3"/>
<Label text="Adresa Kupca" GridPane.columnIndex="0" GridPane.rowIndex="4"/>
<Label text="Email Kupca" GridPane.columnIndex="0" GridPane.rowIndex="5"/>
<Label text="Telefon Kupca" GridPane.columnIndex="0" GridPane.rowIndex="6"/>
<TextField GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<TextField GridPane.columnIndex="1" GridPane.rowIndex="3"/>
<TextField GridPane.columnIndex="1" GridPane.rowIndex="4"/>
<TextField GridPane.columnIndex="1" GridPane.rowIndex="5"/>
<TextField GridPane.columnIndex="1" GridPane.rowIndex="6"/>
<HBox alignment="TOP_RIGHT" GridPane.columnIndex="1" GridPane.rowIndex="7">
<children>
<Button mnemonicParsing="true" text="Save"/>
<Button mnemonicParsing="true" text="Clear"/>
</children>
</HBox>
</children>
<padding>
<Insets bottom="16.0" left="15.0" right="15.0" top="15.0"/>
</padding>
</GridPane>
And here is the main class:
package main;
import java.net.URL;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class test extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
URL fxmlUrl= getClass().getClassLoader().getResource("view.fxml");
GridPane root= FXMLLoader.<GridPane>load(fxmlUrl);
Scene scena = new Scene(root);
stage.setScene(scena);
stage.show();
}
}
Sorry.
Here is another edit for stack trace.
Stack trace is little too big so again ill type some non sense here
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: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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException:
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2504)
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.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at main.test.start(test.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,22]
Message: A pseudo attribute name is expected.
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.setInputSource(XMLStreamReaderImpl.java:214)
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.<init>(XMLStreamReaderImpl.java:184)
at com.sun.xml.internal.stream.XMLInputFactoryImpl.getXMLStreamReaderImpl(XMLInputFactoryImpl.java:262)
at com.sun.xml.internal.stream.XMLInputFactoryImpl.createXMLStreamReader(XMLInputFactoryImpl.java:134)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2478)
... 17 more
Exception running application main.test
C:\Users\Djordje Zlatic\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

Related

javaFx: Error resolving onAction='#switchToScene2', either the event handler is not in the Namespace or there is an error in the script [duplicate]

I'm new in java, so I have to ask you for help :P.
I'm creating very simple program, but I stucked on verifying password and login textField. I'm wondering what is wrong with that code, can you help me?
Controller
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import java.awt.*;
public class Controller {
#FXML
public TextField login,password;
public void LoginButtonClicked(ActionEvent event)
{
if(login.getText().equals("yes")&&password.getText().equals("yes"))
{
System.out.print("Congratulations");
}
else
{
System.out.print("no");
}
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<top>
<VBox BorderPane.alignment="CENTER">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="Profile">
<items>
<MenuItem mnemonicParsing="false" text="Your Profile" />
<MenuItem mnemonicParsing="false" text="Log out" />
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About program" />
<MenuItem mnemonicParsing="false" text="Author" />
</items>
</Menu>
</menus>
</MenuBar>
<HBox spacing="8.0">
<children>
<TextField fx:id="login" promptText="User" /> // ERROR
<TextField fx:id="password" layoutX="10.0" layoutY="10.0" promptText="Password" /> //ERROR
<Region HBox.hgrow="ALWAYS" />
<Button mnemonicParsing="false" onAction="#LoginButtonClicked" text="Log In" />
<Button layoutX="329.0" layoutY="15.0" mnemonicParsing="false" text="Clear" />
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</HBox>
</children>
</VBox>
</top>
<center>
<Label alignment="CENTER" prefHeight="130.0" prefWidth="469.0" text="Log in, in order to use this program" BorderPane.alignment="CENTER">
<font>
<Font size="25.0" />
</font>
</Label>
</center>
</BorderPane>
And the error code
Exception in Application start method
Exception in thread "main" 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$1/868693306.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
/C:/Users/Michael/IdeaProjects/Endomondo/out/production/Endomondo/sample/sample.fxml:36
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3208)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
at sample.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1482523563.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1674404664.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/992465164.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$47/668291877.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$38/1584193862.run(Unknown Source)
... 1 more
Caused by: java.lang.IllegalArgumentException: Can not set java.awt.TextField field sample.Controller.login to javafx.scene.control.TextField
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:758)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1155)
at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:104)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:853)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:747)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2701)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2521)
... 22 more
Your problem is in import in Controller class:
import java.awt.*;
Should be:
import javafx.scene.control.TextField;
The programs tries to convert java.awt.TextField to javafx.scene.control.TextField
I assume you'd like to use javafx, not AWT TextField
Note: don't trust the imports which IDE gives you :)
Check imports, sometimes the IDE will suggest you to import the AWT but since we work with JAVAFX we need to import controls with help of javafx.scene.control.;

Exception running application application

This is my fx class
package application;
import java.io.File;
import java.net.URL;
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("as1.fxml"));
primaryStage.setTitle("Abdul-Rahim University");
primaryStage.setScene(new Scene(root, 800, 500));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
and this is as1.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?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.SeparatorMenuItem?>
<?import javafx.scene.effect.GaussianBlur?>
<?import javafx.scene.effect.MotionBlur?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="abd">
<children>
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New" />
<MenuItem mnemonicParsing="false" text="Open…" />
<Menu mnemonicParsing="false" text="Open Recent" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Save As…" />
<MenuItem mnemonicParsing="false" text="Revert" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Preferences…" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Undo" />
<MenuItem mnemonicParsing="false" text="Redo" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Cut" />
<MenuItem mnemonicParsing="false" text="Copy" />
<MenuItem mnemonicParsing="false" text="Paste" />
<MenuItem mnemonicParsing="false" text="Delete" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Select All" />
<MenuItem mnemonicParsing="false" text="Unselect All" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About MyHelloApp" />
</items>
</Menu>
</menus>
</MenuBar>
<AnchorPane fx:id="ar" maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<BorderPane fx:id="bo" layoutX="69.0" layoutY="32.0" prefHeight="200.0" prefWidth="200.0" />
<HBox fx:id="hb" focusTraversable="true" layoutX="70.0" layoutY="128.0" prefHeight="100.0" prefWidth="420.0" spacing="10.0">
<children>
<Button fx:id="st" mnemonicParsing="false" onAction="#handleButtonAction" text="Students" />
<Button fx:id="ac" mnemonicParsing="false" onAction="#handleButtonAction2" text="Academics" />
<Button fx:id="co" mnemonicParsing="false" onAction="#handleButtonAction3" text="Courses" />
</children>
<opaqueInsets>
<Insets bottom="11.0" left="11.0" right="11.0" top="10.0" />
</opaqueInsets>
<effect>
<GaussianBlur radius="1.75" />
</effect>
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</padding>
</HBox>
<Label fx:id="l1" contentDisplay="BOTTOM" layoutX="236.0" layoutY="26.0" text="University" textAlignment="CENTER" textOverrun="WORD_ELLIPSIS">
<font>
<Font name="Dubai Regular" size="33.0" />
</font>
<effect>
<MotionBlur radius="5.5" />
</effect>
</Label>
</children>
</AnchorPane>
</children>
</VBox>
I have tried to solve it alot and i cant if anyone can help me please solve this problem to me Exception running application applicatio i have tried to solve it alot and i cant if anyone can help me please solve this problem to me Exception running application applicatio
eclipse
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source) Caused by: javafx.fxml.LoadException:
/C:/Users/Abdurrahim/Desktop/mamoun/GUI/bin/application/as1.fxml:18
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
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.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:18)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.ClassNotFoundException: abd
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 22 more Exception running application application.Main
my controller
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
public class FXMLDocumentController {
#FXML
private AnchorPane ar;
#FXML
private BorderPane bo;
#FXML
private HBox hb;
#FXML
private Button st;
#FXML
private Button ac;
#FXML
private Button co;
#FXML
private Label l1;
#FXML
void handleButtonAction(ActionEvent event) {
System.out.println("abd");
}
#FXML
void handleButtonAction2(ActionEvent event) {
System.out.println("abd");
}
#FXML
void handleButtonAction3(ActionEvent event) {
System.out.println("abd");
}
}
javafx
Here is the solution: fx:controller="application.FXMLDocumentController"

onAction="#ClickedClickMe" not working in JavaFX FXML

Good Morning! I have just started ramping up on Java FX and Java FX FXML. I'm using Scene Builder to build the GUI and Netbeans to code the remaining Java FX program.
The issue I'm facing is in FXML File. The Netbeans editor shows an error on the following code in .fxml file for onAction Event.
Error that I see in fxml file:
Controller is NOT defined on root component.
Exception Text:
Executing sample1.jar using platform C:\Program
Files\Java\jdk1.8.0_121\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.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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745) Caused by:
javafx.fxml.LoadException: No controller specified.
file:/C:/Users/hmitty/Documents/NetBeansProjects/sample1/dist/run1317822864/sample1.jar!/sample1/FXMLDocument.fxml:21
at
javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103) at
javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557)
at
javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599)
at
javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532) 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.loadImpl(FXMLLoader.java:3104) at
javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) at
sample1.Sample1.start(Sample1.java:22) at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at
com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
... 1 more Exception running application sample1.Sample1 Picked up
_JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true Java Result: 1
If I create a sample project using Netbeans, it uses AnchorPane and everything seems to be fine. However if I remove AnchorPane and add any other pane and the Button, the onAction Event doesn't work!
I'm a beginner in this language, hence any help/guidance is highly appreciated!
Note: I did my homework for close to half a day in browsing all available solutions as far as I can on StackOverFlow.
The closest I got is this, so any help will definitely move things for me!
https://bugs.openjdk.java.net/browse/JDK-8091582
All the code is pasted below. Let me know if anything else is required!
FXML Code:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.event.ActionEvent?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
<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>
<Button mnemonicParsing="false" onAction="#ClickedClickMe" text="Click Me!" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label fx:id="label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
</GridPane>
Sample1.java
package sample1;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Sample1 extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXMLDocumentController.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 sample1;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class FXMLDocumentController implements Initializable {
#FXML
private Label label;
#FXML
private void ClickedClickMe(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Clicked Try me!!");
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Set controller in fxml file
<GridPane fx:controller="sample1.FXMLDocumentController" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">

JavaFx InvocationTargetException Exception Loading a window

I have a project with 2 windows, the first one pass parameter to the second, but when running the project, the main class doesn't lunch the window, instead I've got this exception:
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: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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
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.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at tic_tac_toe.TIC_TAC_TOE.start(TIC_TAC_TOE.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
here is my main code:
#Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Choices.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Tic Tac Toe");
primaryStage.getIcons().add(new Image("img/icon.png"));
primaryStage.setResizable(false);
primaryStage.show();
}
I've read all the posts about InvocationTargetException but it seems not the same problem, I can't understand where is the error, I've the same code on another project and it works (sorry I'm neww with javaFx, it's just my second project).
here is the FXML file:
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXRadioButton?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="266.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tic_tac_toe.ChoicesController">
<children>
<BorderPane prefHeight="258.0" prefWidth="600.0" AnchorPane.bottomAnchor="51.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<Pane prefHeight="100.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<Label layoutX="170.0" layoutY="23.0" prefHeight="50.0" prefWidth="344.0" text="Veuiller choisr un mode de jeux :">
<font>
<Font size="18.0" />
</font>
</Label>
<ImageView fitHeight="78.0" fitWidth="81.0" layoutX="70.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../img/question-speech-bubble.png" />
</image>
</ImageView>
</children>
</Pane>
</top>
<center>
<Pane prefHeight="97.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<VBox layoutX="29.0" layoutY="8.0" prefHeight="80.0" prefWidth="549.0">
<children>
<JFXRadioButton fx:id="rdJoueur" prefHeight="50.0" text="Un joueur avec une machine">
<font>
<Font size="18.0" />
</font>
</JFXRadioButton>
<JFXRadioButton fx:id="rdMachine" prefHeight="50.0" text="Deux machines">
<font>
<Font size="18.0" />
</font>
</JFXRadioButton>
</children>
</VBox>
</children>
</Pane>
</center>
</BorderPane>
<Pane layoutX="-3.0" layoutY="198.0" prefHeight="58.0" prefWidth="600.0">
<children>
<JFXButton fx:id="btnOk" buttonType="RAISED" layoutX="479.0" layoutY="15.0" prefHeight="35.0" prefWidth="100.0" ripplerFill="#ffffff00" style="-fx-background-color: #2196f3;" text="Valider" textFill="WHITE">
<font>
<Font size="14.0" />
</font>
</JFXButton>
<JFXButton fx:id="btnCancel" buttonType="RAISED" layoutX="360.0" layoutY="15.0" prefHeight="35.0" prefWidth="100.0" ripplerFill="#ffffff00" style="-fx-background-color: #2196f3;" text="Annuler" textFill="WHITE">
<font>
<Font size="14.0" />
</font>
</JFXButton>
</children>
</Pane>
</children>
</AnchorPane>
Thanks
Comment out //
//primaryStage.getIcons().add(new Image("img/icon.png"));

Exception in Application start method

I'm new to javafx and i was trying to make a gui from which when a button is clicked, it would go to another window. I tried reading many answers found on Stack Overflow. I even tried making the project again from the beginning...But i keep on getting the same error.
That is Exception is Application start method
java.lang.reflect.InvocationTargetException javafx
My Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class SkyTravelsFx extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
stage.initStyle(StageStyle.UNDECORATED);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
LoginController.java
package skytravelsfx;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class LoginController implements Initializable {
#FXML
TextField username;
#FXML
TextField password;
#FXML
Button user;
#FXML
Button admin;
#FXML
Button login;
#FXML
public void btnClicked (ActionEvent actionEvent){
try{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("User.fxml"));
Parent root=(Parent) fxmlLoader.load();
Stage stage = new Stage();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.initStyle(StageStyle.UNDECORATED);
stage.show();
((Stage)(((Button)actionEvent.getSource()).getScene().getWindow())).hide();
}catch(Exception e){
e.printStackTrace();
}
}
#Override
public void initialize(URL location, ResourceBundle resources) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Please note that i still haven't written any codes in UserController.java
I just made the ui for User.fxml
Login.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="766.0" prefWidth="1366.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="skytravelsfx.LoginController">
<children>
<ImageView fitHeight="866.0" fitWidth="1397.0" layoutX="10.0" layoutY="11.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../SkyTravelFx/src/goodwp.com_25168.jpg" />
</image>
</ImageView>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<Label fx:id="label1" layoutX="565.0" layoutY="517.0" minHeight="16" minWidth="69" text="Select User">
<font>
<Font size="18.0" />
</font>
</Label>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="#095a77" height="140.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="1386.0" />
<Label fx:id="label11" layoutX="56.0" layoutY="33.0" minHeight="16" minWidth="69" text="Sky Travels" textFill="WHITE">
<font>
<Font name="System Italic" size="72.0" />
</font>
</Label>
<Label fx:id="label12" layoutX="432.0" layoutY="91.0" minHeight="16" minWidth="69" text="Making your destination closer..." textFill="WHITE">
<font>
<Font name="System Italic" size="18.0" />
</font>
</Label>
<AnchorPane layoutX="83.0" layoutY="252.0" prefHeight="507.0" prefWidth="383.0">
<children>
<ImageView fitHeight="191.0" fitWidth="234.0" layoutX="96.0" layoutY="6.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../SkyTravelFx/src/icon-default-profile.png" />
</image>
</ImageView>
<Button fx:id="login" layoutX="153.0" layoutY="440.0" onAction="#handleButtonAction" style="-fx-background-color: #095a77;" text="Log In" textFill="WHITE">
<font>
<Font size="18.0" />
</font>
</Button>
<Label fx:id="label2" layoutX="152.0" layoutY="308.0" minHeight="16" minWidth="69" text="Password">
<font>
<Font size="18.0" />
</font>
</Label>
<Label fx:id="label2" layoutX="150.0" layoutY="214.0" minHeight="16" minWidth="69" text="Username">
<font>
<Font size="18.0" />
</font>
</Label>
<TextField fx:id="username" layoutX="115.0" layoutY="241.0" />
<TextField fx:id="password" layoutX="115.0" layoutY="335.0" />
</children>
</AnchorPane>
<Button fx:id="admin" layoutX="621.0" layoutY="592.0" mnemonicParsing="false" style="-fx-background-color: #095a77;" text="Log In as Admin" textFill="WHITE">
<font>
<Font size="18.0" />
</font>
</Button>
<Button fx:id="user" layoutX="621.0" layoutY="675.0" mnemonicParsing="false" onAction="#btnClicked" prefHeight="39.0" prefWidth="153.0" style="-fx-background-color: #095a77;" text="Log In as User" textFill="WHITE">
<font>
<Font size="18.0" />
</font>
</Button>
</children>
</AnchorPane>
Here is the run-time error i am getting.
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: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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745) Caused by:
javafx.fxml.LoadException: Error resolving
onAction='#handleButtonAction', either the event handler is not in the
Namespace or there is an error in the script.
file:/C:/Users/Dell/Documents/NetBeansProjects/SkyTravelsFx/dist/run1245920268/SkyTravelsFx.jar!/skytravelsfx/Login.fxml:47
at
javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103) at
javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at
javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823) at
javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532) 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.loadImpl(FXMLLoader.java:3104) at
javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) at
skytravelsfx.SkyTravelsFx.start(SkyTravelsFx.java:23) at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at
com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
... 1 more Exception running application skytravelsfx.SkyTravelsFx
Java Result: 1 Deleting directory
C:\Users\Dell\Documents\NetBeansProjects\SkyTravelsFx\dist\run1245920268
jfxsa-run: BUILD SUCCESSFUL (total time: 4 seconds)
The stack trace tells you the problem:
Error resolving onAction='#handleButtonAction', either the event handler is not in the Namespace or there is an error in the script.
You have
<Button fx:id="login" layoutX="153.0" layoutY="440.0" onAction="#handleButtonAction" ... >
but your controller has no method called handleButtonAction. You probably want
<Button fx:id="login" layoutX="153.0" layoutY="440.0" onAction="#btnClicked" ... >

Resources