JAVAFX - Exception caused by Location is not set when loading fxml file - java

I'm trying to integrate my fxml file to my project, using the following code,
final FXMLLoader fxmlLoader =
new FXMLLoader(this.getClass().getResource("/sample.fxml"));
Parent root = (Parent) fxmlLoader.load();
The program crashes at the second line, throwing this exception,
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/1199823423.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)
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 FileActions.start(FileActions.java:42)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/970544503.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1878510174.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/1792840102.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$48/1671111378.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Process finished with exit code 130
I also tried to used the exact path (using copy path), but I still got the same error.
Also tried,
Parent root = FXMLLoader.load(getClass().getResource("../resources/sample.fxml"));
//and
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
and also put it in the same folder of my java files, but none works...
I don't know if this is relevant, but here is my iml file,
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
<root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" exported="" name="commons-net-3.6" level="project" />
<orderEntry type="library" exported="" name="hamcrest-core-1.3" level="project" />
<orderEntry type="library" exported="" name="junit-4.12" level="project" />
</component>
</module>
What causes this and how could I fix it?
Here is a zip of my project if anyone may want to take a look.
Thanks,
Henry

The error seems to indicate that the fxml file is being loaded from a wrong location. getClass().getResource loads a resource which per Java Spec for a Resource is identified by a string consisting of sequence of substrings, delimited by (/) followed by resource name. Each substring must ne a valid Java identifier. There is no true guarantee of resource resolution with .. as it's not a valid a identifier.
Am assuming you are not using Maven folder structure, so I would suggest
simply keeping the fxml file in the same package as that of Java class that launches it and set the path like "sample.fxml"
What do you see when you output getClass().getClassLoader().getResource("sample.fxml")
Also make sure your filename matches with the resource name, no spaces before or after - you can rename and check on the spaces, if any.

I see you are using maven and jetbrains. This exception does indeed come when the fxml file's location is wrong in the code. You are using maven. Maven is searching for resources (like fxml files) in it's resources folder. The maven project's root folder is where pom.xml is. I will reference it as {mavenRoot}.
So let's assume, your fxml file is in this path:
{mavenRoot}/src/main/resources/fxml/sample.fxml
Then you can use theese two lines to set up your fxml loader:
FXMLLoader loader1 = new FXMLLoader();
loader1.setLocation(getClass().getResource("/fxml/sample.fxml"));
In a maven project, the getResource() will search the {mavenRoot}/src/main/resources folder.

Related

Intellij Error:(3, 4) java: modules are not supported in -source 8 (use -source 9 or higher to enable modules)

Edit - this is solved - the issue was solved by setting: "Settings -> Build, Execution, Deployment - Java Compiler -> Project Bytecode Version : 9"
I'm following the example/tutorial at https://www.logicbig.com/tutorials/core-java-tutorial/modules/getting-started-in-intellij.html
This is NOT a maven project. I have set the source level to 9 in the project structure eg
I have set the project level to 9:
I am getting "Error:(3, 4) java: modules are not supported in -source 8
(use -source 9 or higher to enable modules)" error when trying to build/run the project's main class.
And yes I have seen Intellij IDEA 2018.1 Jdk10 cannot run test which is unanswered.
I have just updated Intellij to 2019.2.4 but the issue remains. I am using SDK 11.
How can this be fixed? Thanks.
Edit: the Run/Debug configuration options:
Edit: the error:
Edit: the platform SDK:
Edit: the dependencies panel:
FWIW, the contents of the iml files show language level 9:
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_9" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_9" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="common.widgets" />
</component>
</module>
This is solved - the issue was solved by setting: "Settings -> Build, Execution, Deployment - Java Compiler -> Project Bytecode Version : 9"
Thanks to all who gave their time to try to help.
I think you must be setting SDK under Platform Setting in project structure

Android Studio java-library module classpath NoClassDefFoundError

My project is started to be large, so I decided to split it into separated modules - gui and domain. My domain module mustn't know that it is a part of Android project, so I created java-library module and put some classes there. It works like a charm but there is a problem when I add some other dependencies inside my java-library module like RxJava. I've created a Main class to simply test my code. Builds passes, but when it tries to invoke RxJava classes it crashes with exception:
Exception in thread "main" java.lang.NoClassDefFoundError: io/reactivex/Observable
at com.example.domain.Main.main(Main.java:13)
Caused by: java.lang.ClassNotFoundException: io.reactivex.Observable
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
My Main class:
package com.example.domain;
import io.reactivex.Observable;
public class Main {
public static void main(String[] args) {
Observable.just(1)
.subscribe(integer -> System.out.println(integer));
}
}
My build.gradle file:
apply plugin: 'java-library'
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
api 'io.reactivex.rxjava2:rxjava:2.1.2'
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
1) If I use com.android.library instead it works
2) If I add RxJava library as a .jar file it changes nothing
EDIT:
I looked into Domain.iml file and I found something strange:
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":Domain" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":Domain" />
</configuration>
</facet>
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="true" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/build/classes/java/main" />
<output-test url="file://$MODULE_DIR$/build/classes/java/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" scope="PROVIDED" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="reactive-streams-1.0.0" level="project" />
</component>
</module>
The weird part is scope attribute in orderEntry node
<orderEntry type="library" exported="" scope="PROVIDED" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="reactive-streams-1.0.0" level="project" />
If I remove it manually:
<orderEntry type="library" exported="" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" name="reactive-streams-1.0.0" level="project" />
It works like a charm.
Unfortunately if I sync my build.gradle file it overrides my modification.
How to make it works permanently and why this acts like this?
The weird part is scope attribute in orderEntry node
<orderEntry type="library" exported="" scope="PROVIDED" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="reactive-streams-1.0.0" level="project" />
If I remove it manually:
<orderEntry type="library" exported="" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" name="reactive-streams-1.0.0" level="project" />
It works like a charm.
Unfortunately if I sync my build.gradle file it overrides my
modification.
Have you tried to change the scope via Android Studios Project Structure?
File -> Project Structure then select the Dependencies Tab and add the Dependency for the required module. In your case that would be java-library I believe.
As far as i know you can't change the scope of the dependency once added. So if you have already added it, try to remove and add it again. Be sure to assign the implementation scope though and see if it works.
I hope this was helpful

Changing from FileReader to getResourceAsStream

When I tried loading my little game from a .jar file the fileReader would no longer work and I was advised to use the getResourceAsStream function instead, when I run the code bellow in the IDE it works fine so the path is correct, when I change to the getResourceAsStream which is currently commented out and remove the file reader line, I get these errors.
Exception in thread "Thread-0" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
Is there a way that I can fix this?
public class Utils {
public static String loadFileAsString(String path) {
StringBuilder builder = new StringBuilder();
try{
//InputStream is = Utils.class.getResourceAsStream(path);
//BufferedReader br = new BufferedReader(new InputStreamReader(is));
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
while((line = br.readLine()) != null)
builder.append(line + "\n");
br.close();
}catch(IOException e){
e.printStackTrace();
}
return builder.toString();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/res" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="file://$MODULE_DIR$/res" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="file://$MODULE_DIR$/res/worlds" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
You need to start understanding what you're doing, what the classpath is, how Java loads classes, and how an app is bundled.
When you start a Java program, you use a command looking like this:
java -cp /some/directory:/some/file.jar com.foo.bar.Main
Given the above command, java will try loading the main class (and all the other classes used in your code) in two locations:
the /some/directory directory
the jar file /some/file.jar
Since the class is named Main, and is in the package com.foo.bar, it will thus look for a file named /some/directory/com/foo/bar/Main.class and if it doesn't find it, it will look inside the jar file for an entry /com/foo/bar/Main.class.
When you use SomeClass.getResourceAsStream("/res/worlds/world1.txt"), it will apply the same strategy: look for a file /some/directory/res/worlds/world1.txt and if not found, look inside the jar file for an entry /res/worlds/world1.txt.
So, since you use an IDE to run the project, you need to understand how an IDE works: what command does it use to start your program. The principle is quite simple:
there is a target directory where the IDE puts the .class files it generates from the source .java files. This target directory is part of the classpath when running the program for the IDE
there is also a target directory where the IDE stores all the files that are not .java files, and are located under a directory in IntelliJ marked as "Resources root". This target directory is also part of the classpath.
The structure in the target directory always respects the structure of the source/resource directory.
When packaging the app as a jar file, you'll end up with a jar file containing everything located under these target directories.
By default, in a basic IntelliJ project, your src directory is also a resources root. So every file you put there ends up in the target directory, and is thus available in the classpath, both when launching from the IDE, and when the target directory is packaged as a jar. You should just put the file somewhere under the src directory, and not mess with dependencies. Your txt file is part of the sources of the project, just like a Java file. It's "compiled" by the IDE by copying it to the directory, along with the .class files.

IzPack Shortcut Nightmare

I'm at my wit's end with this.
I have a VERY simple installation descriptor for izpack for a two-package Java app on Windows. Everything works as intended EXCEPT for the shortcut creation. The shortcut panel doesn't seem right, for a start. The label "ShortcutPanel.regular.startup" is displayed instead of "run at startup" or anything of the sort in the startup execution checkbox.
After running the installer, the shortcuts simply aren't created. Here's what my install.xml looks like, it's pretty straightforward:
<izpack:installation version="5.0"
xmlns:izpack="http://izpack.org/schema/installation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://izpack.org/schema/installation http://izpack.org/schema/5.0/izpack-installation-5.0.xsd">
<info>
<appname>application_name</appname>
<appversion>2.0.0</appversion>
<appsubpath>myapp</appsubpath>
<javaversion>1.8</javaversion>
</info>
<locale>
<langpack iso3="bra" />
</locale>
<resources>
<res src="shortcutSpec.xml" id="shortcutSpec.xml"/>
</resources>
<variables>
<variable name="TargetPanel.dir.windows" value="C:/Sistemas"/>
</variables>
<guiprefs width="800" height="600" resizable="no">
<laf name="substance">
<os family="windows" />
<os family="unix" />
<param name="variant" value="mist-silver" />
</laf>
<modifier key="useHeadingPanel" value="yes" />
</guiprefs>
<panels>
<panel classname="HelloPanel" />
<panel classname="DefaultTargetPanel" />
<panel classname="ShortcutPanel" />
<panel classname="InstallPanel" />
<panel classname="FinishPanel" />
</panels>
<natives>
<native type="izpack" name="ShellLink.dll">
<os family="windows"/>
</native>
<native type="izpack" name="ShellLink_x64.dll">
<os family="windows"/>
</native>
</natives>
<packs>
<pack name="Pack1" required="yes">
<description>one of the packs it's a jar</description>
<file src="lib/pack1.jar" targetdir="$INSTALL_PATH/pack1subdir"
override="true">
</file>
<file src="imagens/logo.ico" targetdir="$INSTALL_PATH/pack1subdir/imagens/"
override="true" />
<executable targetfile="lib/pack1.jar" type="jar"
stage="never"></executable>
</pack>
<pack name="Pack2" required="yes">
<description>the other pack</description>
<file src="lib/pack2.jar" targetdir="$INSTALL_PATH/pack2subdir"
override="true">
</file>
<file src="imagens/update.ico" targetdir="$INSTALL_PATH/pack2subdir/imagens/"
override="true" />
<executable targetfile="lib/pack2.jar" type="jar"
stage="never"></executable>
</pack>
</packs>
Then there's the shortcutSpec.xml. My pom moves it to my staging directory:
<izpack:shortcuts version="5.0"
xmlns:izpack="http://izpack.org/schema/shortcuts" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://izpack.org/schema/shortcuts http://izpack.org/schema/5.0/izpack-shortcuts-5.0.xsd">
<lateShortcutInstall />
<shortcut name="pack1" programGroup="false"
desktop="true" applications="false" startMenu="no" startup="true"
target="java -jar $INSTALL_PATH/pack1subdir/pack1.jar"
workingDirectory="$INSTALL_PATH/pack1subdir/">
<createForPack name="pack1" />
</shortcut>
<shortcut name="pack2" programGroup="false"
desktop="true" applications="false" startMenu="no" startup="true"
target="java -jar $INSTALL_PATH/pack2subdir/pack1.jar"
workingDirectory="$INSTALL_PATH/pack2subdir/">
<createForPack name="pack2" />
</shortcut>
The installer simply doesn't create any shortcuts anywhere. It finishes with no error messages no logs no stack traces no nothing.
Help.
I fixed that by going into the izPack/lib/izpack-core-5.0.9.jar and opening the /com/izforge/izpack/bin/langpacks/installer/ folder in it. Then editing the bra.xml file and adding the following line:
<str id="ShortcutPanel.regular.startup" txt="Iniciar com o Windows"/>
Then save and update the file in the jar and rebuild your instalation.

How can I tell a Cursive Clojure module to depend on a local Java/Scala module?

I imported a Leiningen project into Intellij to sit alongside some existing Java & Scala modules. I would like to call functions from those modules from my Clojure module, but I'm not sure how to define this dependency. I went to Project Settings -> Modules and the "Dependencies" tab that's usually there is missing, leading me to believe that I'm not allowed to express dependencies anywhere other than the Leiningen project file (I've played with the Leiningen project editor and can't figure out how to do this there either...).
Here is the .iml file if anyone is curious how Intellij is viewing this module.
<?xml version="1.0" encoding="UTF-8"?>
<module cursive.leiningen.project.LeiningenProjectsManager.displayName="testproject:0.1.0-SNAPSHOT" cursive.leiningen.project.LeiningenProjectsManager.isLeinModule="true" type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/classes" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/dev-resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Leiningen: clojure-complete:0.2.3" level="project" />
<orderEntry type="library" name="Leiningen: org.clojure/clojure:1.6.0" level="project" />
<orderEntry type="library" name="Leiningen: org.clojure/tools.nrepl:0.2.10" level="project" />
I develop Cursive. I suspect there may not be a good solution for this right now. I'll look at the code and see if I can figure out a way to do it, but it'll require knowledge of how Maven, Gradle or SBT (in your case) represent the modules internally. I'll try to look at this today and let you know. I actually don't know if it's possible in IntelliJ for a Maven project to depend on an SBT project, for example, or a Gradle one.
In the meantime, publishing to your local repo is the only workaround I can think of. Sorry, I know it's clunky.
There's no difference from IntelliJ's point of view between a "normal" project and an IntelliJ one, it's just that Cursive will rewrite a lot of the module configuration on each sync since in theory that configuration should be managed by Leiningen. This is more or less how the Maven integration works.
So, I eventually was able to do this... as dAni suggested, I was able to create a new Clojure project (through IntelliJ this time -- previously I did "lein new clj-test" then tried to import this into IntelliJ), create a Leiningen build file, process that build file, and then see the Dependencies tab for the module in Project Structure.
Still, the REPL could not find the classes. So I had to edit the Run configuration to use nREPL in "a normal JVM process" which let me select the module whose classpath I would presumably be using.
Hate to be the guy to accept my own answer (especially since this has some limitation... I want to use this with Gorilla REPL in the end so will probably devise a different strategy for that) but I think this answers the original question pretty well, hope it helps someone. I think essentially what the above strategy is doing is using Leiningen for dependency resolution but not actually for the REPL, so Leiningen's dependencies are available on the classpath but the REPL is launched some other way via IntelliJ.

Categories