problem in loading an file in java fx in netbeans ide - javafx

I'm trying to load an image file in javafx main class. i've commented all the lines in my class but still getting an error. i think the image is not loading from the path. What should be done in this matter?
Image img=new Image("‪\"\\\\C:\\\\Users\\\\Avi\\\\Desktop\\\\test.jpg\"");
sp_mainlayout = new StackPane();
cc_custom = new CustomControl();
Pane back=new HBox(15);
sp_mainlayout.getChildren().add(cc_custom);
back.getChildren().add(new ImageView(img));
}
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Chess game");
primaryStage.setScene(new Scene(sp_mainlayout, 1000, 1020));
primaryStage.setMinWidth(300);
primaryStage.setMinHeight(300);
primaryStage.setResizable(false);
primaryStage.show();
}
The exception the programme is showing.
Exception in Application init 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 init method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:912)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1118)
at javafx.scene.image.Image.<init>(Image.java:620)
at project.template.ChessApplication.init(ChessApplication.java:52)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:841)
... 2 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1110)
... 5 more
Exception running application project.template.ChessApplication
Java Result: 1

Related

JAVAFX Problem trying to play sound when clicking button

I'm trying to make a sound of Windows play when the user clicks the button. The code is just below:
public class TestController extends Application {
public String audio = getClass().getResource("src/Sounds/WindowsError.wav").toString();
#FXML
private Button playbt;
#FXML
void playtest(MouseEvent event)
{
System.out.println("Clicked!");
AudioClip clip = new AudioClip(audio);// 1
clip.play(); // 2
}
#Override
public void start(Stage primaryStage) {
try
{
FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/FXML/Test.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
The controller is properly configured to FXML. However, when I try to run the project, the following error occurs.
Exception in Application constructor 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: Unable to construct Application instance: class Controller.TestController
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$165(LauncherImpl.java:819)
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.NullPointerException
at Controller.TestController.<init>(TestController.java:17)
... 13 more Exception running application Controller.TestController
The files in my project are arranged as follows:
Does anyone know why this error occurs? The project hangs when opening due to the sound playback code.
I'm not totally solid on this, but I think the syntax of the getResource for the audio variable is in "relative address" form since it doesn't start with "/". In other words, it could be looking for a subfolder src in Controller package folder.
Maybe the following will help the system locate your audio resource:
public String audio = getClass().getResource("../Sounds/WindowsError.wav").toString();
Perhaps it would be good to move this line into the start() method, and run a System.out.println(audio) to inspect what you have at that point.
EDIT: just saw fabian's comment. Changing to the form using the classpath root as he suggests might be a better general practice than my suggestion to use a relative path.

Groovy with JavaFX GroovyObject not found in extractor for ObservableList

I'm trying to have an ObservableList of some type User which will notify any listchangelistener if one of the items stored in it had any of its values changed.
Though the callback always throws the 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$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
at org.codehaus.groovy.runtime.ProxyGeneratorAdapter$InnerLoader.defineClass(ProxyGeneratorAdapter.java:926)
at org.codehaus.groovy.runtime.ProxyGeneratorAdapter.<init>(ProxyGeneratorAdapter.java:191)
at groovy.util.ProxyGenerator.createAdapter(ProxyGenerator.java:233)
at groovy.util.ProxyGenerator.instantiateDelegateWithBaseClass(ProxyGenerator.java:208)
at groovy.util.ProxyGenerator.instantiateDelegateWithBaseClass(ProxyGenerator.java:192)
at groovy.util.ProxyGenerator.instantiateDelegate(ProxyGenerator.java:184)
at groovy.util.ProxyGenerator.instantiateDelegate(ProxyGenerator.java:180)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.asType(DefaultGroovyMethods.java:15768)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.asType(DefaultGroovyMethods.java:10859)
at org.codehaus.groovy.runtime.dgm$53.doMethodInvoke(Unknown Source)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1225)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1034)
at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:935)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:926)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:181)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.asType(ScriptBytecodeAdapter.java:604)
at sample.Main$1.call(Main.groovy:36)
at sample.Main$1.call(Main.groovy)
at com.sun.javafx.collections.ElementObserver.attachListener(ElementObserver.java:79)
at com.sun.javafx.collections.ObservableListWrapper.doAdd(ObservableListWrapper.java:100)
at javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:151)
at java.util.AbstractList.add(AbstractList.java:108)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
at javafx.collections.ModifiableObservableListBase.addAll(ModifiableObservableListBase.java:99)
at javafx.collections.ObservableListBase.addAll(ObservableListBase.java:245)
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 org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:192)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)
at sample.Main.start(Main.groovy:39)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
... 1 more
Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:702)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:812)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:800)
... 47 more
Exception running application sample.Main
I was able to reproduce the error with this example:
package sample
import javafx.application.Application
import javafx.beans.Observable
import javafx.collections.FXCollections
import javafx.collections.ObservableList
import javafx.scene.Scene
import javafx.scene.control.ComboBox
import javafx.stage.Screen
import javafx.stage.Stage
import javafx.util.Callback
class Main extends Application {
static void main(String[] args) {
launch(Main.class, args)
}
class User {
String name
User(String t){
name = t
}
}
#Override
void start(Stage primaryStage) {
ObservableList<User> usersTest =
FXCollections.observableArrayList(new Callback<User, Observable[]>() {
#Override
Observable[] call(User o) {
return [o] as Observable
}
})
usersTest.addAll( new User("John"), new User("Jane"))
ComboBox<User> cb = new ComboBox<>()
cb.setItems(usersTest)
cb.value = cb.items.first()
def bounds = Screen.primary.bounds
Scene scene = new Scene(cb, bounds.width, bounds.height)
primaryStage.setScene(scene)
primaryStage.show()
}
}
I'm honestly a bit out of options as the error is as unhelpful as it gets.

FXML files couldn't be found after building the project [duplicate]

This question already has answers here:
JAVAFX: Location is not set error [duplicate]
(2 answers)
Closed 6 years ago.
In order to learn JavaFX I created my first project using IntelliJ IDEA and exported the project as an Eclipse project. Everything is working well when I run the project in those two IDEs. Unfortunately, after building the project and trying to run the .jar or the .exe file I get an exception and I think that the problem is in the FXMLLoader which is unable to find the fxml files.
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("../view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
// Give the controller access to the main app.
RootLayoutController rootLayoutController = loader.getController();
rootLayoutController.setMain(this);
} catch (IOException e) {
showExceptionDialog(e);
}
// Try to load last opened person file.
File file = getPersonFilePath();
if (file != null) {
loadPersonDataFromFile(file);
}
}
Project hierarchy
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$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at com.melkojji.controller.Main.initRootLayout(Unknown Source)
at com.melkojji.controller.Main.start(Unknown Source)
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 com.melkojji.controller.Main
I don't think your fxml files are being packaged with the jar. You should use a resources folder. Go to your projects settings and set the module's resource folder.

Designing an API - troubles with class loading (Groovy)

It's my first time designing an API. I have a program with folder 'plugins'. I made a test plugin in that folder packaged in JAR. In the program I have a Groovy class 'Configuration', which is supposed to load the plugins.
Configuration.java:
static void processConfiguration()
{
File pluginDirectory=new File(PLUGINDIRECTORY)//it's 'plugins'
if(!pluginDirectory.exists()) pluginDirectory.mkdir()
File[] pluginfiles=pluginDirectory.listFiles()
for(File f:pluginfiles)
{
if(f.name.endsWith('.jar'))
{
JarFile jar=new JarFile(f)
for(JarEntry jarEntry:jar.entries())
{
if(jarEntry.getName().endsWith('.class'))
{
//try loading the class
}
}
}
}
}
So, I tried doing these at the //try loading the class block
Class cl=ClassLoader.getSystemClassLoader().loadClass(jarEntry.name)
Class cl=new GroovyClassLoader().loadClass(jarEntry.name)
Class cl=Configuration.classLoader.loadClass(jarEntry.name)
Class cl=Class.forName(jarEntry.getName())
also tried with replacing - jarEntry.name.replaceAll('/','.').replace('.class',''), but I think it's done automatically.
Getting Class not found 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.ClassNotFoundException: jace/plugins/JavaPlugin.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java_lang_ClassLoader$loadClass.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at jace.Configuration.processConfiguration(Configuration.groovy:87)
at jace.Jace.start(Jace.java:56)
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.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
After some searching and tinkering I finally resolved it.
First, need to create a list of proper URLs;
Second, make a list of classes from the jar;
Third, instantiate GroovyClassLoader and give him all URLs;
Fourth, load classes from list, check whether they implement plugin interfaces, then instantiate them.
static void processConfiguration()
{
File pluginDirectory=new File(PLUGINDIRECTORY)
if(!pluginDirectory.exists()) pluginDirectory.mkdir()
File[] pluginfiles=pluginDirectory.listFiles()
URL[] urls=[]
ArrayList<String> classes=new ArrayList<>()
for(File f:pluginfiles)
{
if(f.name.endsWith('.jar'))
{
JarFile jar=new JarFile(f)
URL url=new URL('jar:file:'+PLUGINDIRECTORY+'/'+f.name+'!/')
urls+=url
jar.entries().each {if(it.name.endsWith('.class'))classes.add(it.name) }
}
}
// println(gcl.loadedClasses)
GroovyClassLoader groovyClassLoader=new GroovyClassLoader()
urls.each {groovyClassLoader.addURL(it)}
// println(classes)
classes.each {
Class cl=groovyClassLoader.loadClass(it.replaceAll('/','.').replace('.class',''))
Class[] interfaces=cl.getInterfaces()
if(interfaces.contains(Plugin.class))
{
Object instance=cl.newInstance()
if(interfaces.contains(SuggestionPlugin.class))
{
SuggestionProcessor.suggestionPlugins.add(instance as SuggestionPlugin)
}
Constructor[] constructors=cl.getConstructors()
println("Loaded a plugin $cl.simpleName")
}
}
}

Java FXML resource FXMLDocument.fxml

public class Clearance 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();
}
public static void main(String[] args) {
launch(args);
}
}
Clearance.java and FXMLDocument.fxml are in the same package.
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:497)
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:497)
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$156(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
FXMLDocument.fxml:12
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.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 clearance.Clearance.start(Clearance.java:23)
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)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$50(GtkApplication.java:139)
... 1 more
Caused by: java.lang.NullPointerException
at clearance.FXMLDocumentController.<init>(FXMLDocumentController.java:28)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
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)
... 17 more
Exception running application clearance.Clearance
Java Result: 1
I guess your FXMLDocument.fxml contains a line similar to fx:controller="clearance.FXMLDocumentController"? When loading the FXML file, the corresponding controller is instantiated using reflection.
Thus you need to make sure, your Controller has a visible default constructor, which doesn't throw any exception. But in your case it seems like it threw a NullPointerException.

Resources