getClass().getResource() throws a NullPointerException [duplicate] - java

This question already has an answer here:
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 9 months ago.
I'm currently trying to start coding an app with IntelliJ and JavaFX, I have the following code :
package main.gui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;
public class MainMenu extends Application {
#Override
public void start(Stage stage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("hello-view.fxml"));
URL url = getClass().getResource("hello-view.fxml");
System.out.println("URL = " + url);
Parent root = FXMLLoader.load(url);
Scene scene = new Scene(root,1920,1080);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
}
My file structure :
src/main/gui/MainMenu.java
src/main/gui/hello-view.fxml
And I get this exception :
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1082)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at main/main.gui.MainMenu.start(MainMenu.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
It outputs : URL = null
I've tried a lot of stuff that I've found on this site but without success...
Thank in advance for your help :)

To add a little more detail : I'm using Maven and I created the project with the
JavaFX project creation tool provided with IntelliJ.
I made it work by simply putting my fxml file into a ressources folder (src/main/resources) as suggested by #MatteoNNZ.
I tweaked a little bit the code to have the following :
package main.gui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;
public class MainMenu extends Application {
#Override
public void start(Stage stage) throws Exception {
URL url = getClass().getClassLoader().getResource("hello-view.fxml");
System.out.println("URL = " + url);
Parent root = FXMLLoader.load(url);
Scene scene = new Scene(root,1920,1080);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
}
Obviously it could be simplified like this :
#Override
public void start(Stage stage) throws Exception {
// URL url = getClass().getClassLoader().getResource("hello-view.fxml"); |
// System.out.println("URL = " + url); | Remove these 3 lines
// Parent root = FXMLLoader.load(url); |
Scene scene = new Scene(FXMLLoader.load(getClass().getClassLoader().getResource("hello-view.fxml")),1920,1080);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
but it is very ugly.
Thank you everyone for your help and very quick answers :) .

Related

Receiving an error in Java and don't understand the problem: Exception in Application init method java.lang.reflect.InvocationTargetException

I'm developing a program using JavaFX and I see no errors in my code (not underlined), yet when I run the code using a seperate file, I am getting the following error:
Here is the full stack trace:
Exception in Application init 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 init method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:912)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
at website.CreateAccountPane.<init>(CreateAccountPane.java:60)
at website.WebsiteRootPane.<init>(WebsiteRootPane.java:22)
at main.ApplicationLoader.init(ApplicationLoader.java:14)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:841)
... 2 more
Exception running application main.ApplicationLoader
This is the code for my application launcher:
package main;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import website.WebsiteRootPane;
public class ApplicationLoader extends Application {
private WebsiteRootPane root;
#Override
public void init() {
root = new WebsiteRootPane();
}
#Override
public void start(Stage stage) throws Exception {
stage.setMinWidth(1000);
stage.setMinHeight(750);
stage.setTitle("Planet Generator");
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is one of the tab panes for my website, so far it is the only one I've built.
package website;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class CreateAccountPane extends GridPane {
private ComboBox<String> cboTitle;
private TextField txtSurname, txtFirstName, txtEmail;
private Button btnCreate;
private PasswordField pwrd;
public CreateAccountPane() {
//create labels
Label lblTitle = new Label("Title");
Label lblFirstName = new Label("First name");
Label lblSurname = new Label("Surname");
Label lblEmail = new Label("Email");
Label lblPassword = new Label("Password");
// setup combobox
ObservableList<String> titles = FXCollections.observableArrayList("Mr", "Mrs", "Miss", "Ms");
cboTitle = new ComboBox<String>(titles);
cboTitle.getSelectionModel().select(0);
// setup text fields
txtFirstName = new TextField();
txtSurname = new TextField();
txtEmail = new TextField();
pwrd = new PasswordField();
this.add(lblTitle, 0, 0);
this.add(cboTitle, 1, 0);
this.add(lblFirstName, 0, 1);
this.add(txtFirstName, 1, 1);
this.add(lblSurname, 0, 2);
this.add(txtSurname, 1, 2);
this.add(lblEmail, 0, 3);
this.add(txtEmail, 1, 3);
this.add(lblPassword, 0, 4);
this.add(pwrd, 1, 4);
this.getChildren().add(txtFirstName);
}
}
The problem
You are adding txtFirstName twice but you can only add an element to the UI once.
You add it with
this.add(txtFirstName, 1, 1);
and
this.getChildren().add(txtFirstName);
You should be able to fix it by removing
this.getChildren().add(txtFirstName);
How to find that (read that, it will help you :))
Look at your exception:
Exception in Application init method java.lang.reflect.InvocationTargetException
This says that the problem occurs while executing the code of init.
InvokedTargetException means that a method invoked using reflection(what is the case with init as JavaFX uses reflection for this) threw an exception. This could be any exception. The Caused by section in the stack trace shows the real exception. Let's look at it:
Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT
at javafx.scene.Parent$2.onProposedChange(Parent.java:454)
at com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
at website.CreateAccountPane.(CreateAccountPane.java:60)
The exception message (Children: duplicate children added: parent = Grid hgap=0.0, vgap=0.0, alignment=TOP_LEFT) tells you that the problem is that you have already added that element.
at website.CreateAccountPane.(CreateAccountPane.java:60)
tells you that it occured in line 60 of CreateAccountPane(CreateAccountPane.java:60).
Next, we look closer at this line:
this.getChildren().add(txtFirstName);
It actually adds an element to the grid pane.
So, it seems that this element is already a part of the grid pane. So, let's see what you've added to it previously:
this.add(txtFirstName, 1, 1);
You added the same element two times. That is the problem.
Conclusion
As you see, reading and interpreting stack traces is a very, very important part of programming. If you go on, you will see more and more stack traces like this.
Don't fear those huge red texts. Split them apart and try to find the problem.
This may be time consuming and difficult, especially at the beginning but with time, you will get better!
Finding, understanding and fixing bugs is a very important part of programming(maybe the most important part).
This part will be much easier if you can
read and interpret exceptions
and
debug. If you don't know what debugging is, look it up, learn it and try it out. It will almost surely help you.

Why it shows illegal character: '\ufeff' while importing javaFX stage [duplicate]

I have a main class which should call a JavaFX application (SimpleSun) to get Information from the user. Currently I create an Object of the JavaFX class and start it, but that doesn't seem to work. Does someone see the mistake in my work?
Here's my code and exception:
Main.java:
package ch.i4ds.stix.sim;
import ch.i4ds.stix.sim.grid.config.Configuration;
import ch.i4ds.stix.sim.grid.config.ConfigurationFromFile;
public class Main{
Configuration config;
public static void main(String[] args) {
ConfigurationFromFile config = new ConfigurationFromFile();
SimpleSun ss = new SimpleSun(config);
ss.show();
}
}
SimpleSun.java:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import ch.i4ds.stix.sim.grid.config.Configuration;
import ch.i4ds.stix.sim.grid.config.ConfigurationFromFile;
public class SimpleSun extends Application{
private Stage primaryStage;
Configuration configuration;
public SimpleSun(ConfigurationFromFile config) {
this.configuration = config;
}
#Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Simple Sun - Alpha");
System.out.println("Test");
try {
// Load the root layout from the fxml file
FXMLLoader loader = new FXMLLoader(
Main.class.getResource("view/RootLayout.fxml"));
BorderPane rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
public void show(){
launch();
}
}
Exception:
Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class ch.i4ds.stix.sim.SimpleSun
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: ch.i4ds.stix.sim.SimpleSun.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
... 4 more
You must provide a constructor with no arguments when you extend application. So you could do something like:
public class SimpleSun extends Application {
private Stage primaryStage;
Configuration configuration;
public SimpleSun() {
this.configuration = Main.getConfig();
}
//...
and in your Main class:
public static Configuration getConfig() { return new ConfigurationFromFile(); }
Alternatively you can pass String parameters to the class with launch(args) and get them back in the SimpleSun class with getParameters().

Program will not run when turned into .jar

I have tried many methods over the last few days with no success...I've trawled through various Stackoverflow entries and to no avail...I must be missing something.
I have tried across three different IDEs...IntelliJ, Eclispe and Netbeans.
The problem is when trying to turn my program into an executable jar it is unable to run (either by double clicking or running through command).
When executing the following on command:
java -jar D:\Computing\Programming\Java\Projects\JavaFXGameMenu\out\artifacts\JavaFXGameMenu_jar\JavaFXGameMenu.jar
I get: Error: Could not find or load main class root.Main
When i run the same but with javaw instead.. i get not error message, but nothing happens either.
I am predominately using IntelliJ as its a JavaFX application that I am building.
This is the project hierarchy:
When creating the Artifact I choose the following based upon other threads:
I then re run this using: Java -jar D:\Computing\Executables\JavaFXGameMenu.jar
I get the following issue:
I have put the relevant environment variables into my system and Path and I am using jre1.8.0_144.
Any help with tracking down what the problem could be is greatly appreciated
Code below...but this fully compiles and runs within IDE without errors...the problem is when its turned into a .jar and ran from command.
package root;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Main extends Application {
private double width = 1920;
private double height = 1080;
private Parent createContent(){
Pane root = new Pane();
root.setPrefSize(width, height); //W:860 H:600
ImageView imgLogo = null;
ImageView bottomlogo = null;
MenuItem newGame = new MenuItem("NEW GAME");
MenuItem continueGame = new MenuItem("CONTINUE");
MenuItem friends = new MenuItem("FRIENDS");
MenuItem settings = new MenuItem("SETTINGS");
MenuItem store = new MenuItem("STORE");
MenuItem exit = new MenuItem("EXIT");
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/dark.jpg"))) {
ImageView img = new ImageView(new Image(is));
img.setFitWidth(width);
img.setFitHeight(height);
root.getChildren().add(img);
} catch (IOException e){
System.out.println("Couldn't Load Image");
}
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/logo.png"))) {
imgLogo = new ImageView(new Image(is));
imgLogo.setX(1000);
imgLogo.setY(100);
imgLogo.setFitWidth(600);
imgLogo.setFitHeight(300);
} catch (IOException e){
System.out.println("Couldn't Load Image");
}
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/SteamAgony.png"))) {
bottomlogo = new ImageView(new Image(is));
bottomlogo.setX(100);
bottomlogo.setY(800);
bottomlogo.setFitHeight(200);
bottomlogo.setFitWidth(200);
bottomlogo.setOpacity(0.7);
} catch (IOException e){
System.out.println("Couldn't Load Image");
}
MenuBox menu = new MenuBox(
newGame,
continueGame,
friends,
settings,
store,
exit);
menu.setTranslateX(width / 3.4);
menu.setTranslateY(height / 2.5);
settings.setOnMouseClicked(event -> new SceneCreator().createScene(200,300));
exit.setOnMouseClicked( event -> Platform.exit());
root.getChildren().addAll(menu, imgLogo, bottomlogo);
return root;
}
#Override
public void start(Stage primaryStage) throws Exception{
Scene scene = new Scene(createContent());
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
}
#Override
public void stop(){
//TODO
}
public static void main(String[] args) {
launch(args);
}
}
The error is at Main.createContent line 102. Maybe you forgot to initialize the child node (I'm not really familiar with JavaFX).
As #cedrikk mentioned, the problem is related to your code in Main.createContent.
You said the problem is when trying to run the jar as an executable -
did you try to run it within the IDE? If not - you should try to, and while at it - debug it to help you find the problem. Just right click your Main class and choose debug.
Regarding running it with javaw, the reason you got no error messages is because javaw executes java programs without a console - where you would normally get error messages unless using some logging solution.
You are adding a null element as a child of a Pane, causing the null pointer issue you are facing, if you use the debugger you'll be able to find where the variable that shouldn't be null is being set to null.
Edit - after code added
You are seeing 2 fields in try catches (imageLogo, bottomLogo), and adding them even if they fail, which adds nulls, causing the error.
You use a relative path for the images, this is probably the issue, are you running the jar from the project root? If not you could put the absolute path, but using resources would be more reliable, and allow the jar to run on different computers.
The problem was down to declaring inputStreams.
This works for both in IDE and running from jar:
String bgImg = "root/resources/dark.jpg";
URL bgImgPath = Main.class.getClassLoader().getResource(bgImg);
ImageView img = new ImageView(new Image(bgImgPath.toExternalForm()));
img.setFitWidth(width);
img.setFitHeight(height);
root.getChildren().add(img);
Compared to what I had before:
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/dark.jpg"))){
ImageView img = new ImageView(new Image(is));
img.setFitWidth(width);
img.setFitHeight(height);
root.getChildren().add(img);
}catch (IOException e) {
System.out.println("Could not load");
}
The changes to the paths, were from where I tried adding the resource folder to the root folder instead of within src.

Trying to call a JavaFX application from Java... NoSuchMethodException

I have a main class which should call a JavaFX application (SimpleSun) to get Information from the user. Currently I create an Object of the JavaFX class and start it, but that doesn't seem to work. Does someone see the mistake in my work?
Here's my code and exception:
Main.java:
package ch.i4ds.stix.sim;
import ch.i4ds.stix.sim.grid.config.Configuration;
import ch.i4ds.stix.sim.grid.config.ConfigurationFromFile;
public class Main{
Configuration config;
public static void main(String[] args) {
ConfigurationFromFile config = new ConfigurationFromFile();
SimpleSun ss = new SimpleSun(config);
ss.show();
}
}
SimpleSun.java:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import ch.i4ds.stix.sim.grid.config.Configuration;
import ch.i4ds.stix.sim.grid.config.ConfigurationFromFile;
public class SimpleSun extends Application{
private Stage primaryStage;
Configuration configuration;
public SimpleSun(ConfigurationFromFile config) {
this.configuration = config;
}
#Override
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("Simple Sun - Alpha");
System.out.println("Test");
try {
// Load the root layout from the fxml file
FXMLLoader loader = new FXMLLoader(
Main.class.getResource("view/RootLayout.fxml"));
BorderPane rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
public void show(){
launch();
}
}
Exception:
Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class ch.i4ds.stix.sim.SimpleSun
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: ch.i4ds.stix.sim.SimpleSun.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
... 4 more
You must provide a constructor with no arguments when you extend application. So you could do something like:
public class SimpleSun extends Application {
private Stage primaryStage;
Configuration configuration;
public SimpleSun() {
this.configuration = Main.getConfig();
}
//...
and in your Main class:
public static Configuration getConfig() { return new ConfigurationFromFile(); }
Alternatively you can pass String parameters to the class with launch(args) and get them back in the SimpleSun class with getParameters().

connecting scenebuilder UI fxml with javafx application

I have designed a UI in javafx scene builder, which has a simple button in stackpane.And I have named the controller class as simplecclass. I have saved the fxml as simple.fxml.
I have created a controller class in netbeans, which simply prints some msg on clicking the button.
In the NewFXBuilder java , I have loaded simple.fxml. Please find below the NewFXBuilder.java code.
package javafxapplication2;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
public class NewFXbuilder extends Application {
#Override
public void start(Stage primaryStage) {
try {
StackPane page = (StackPane) FXMLLoader.load(NewFXbuilder.class.getResource("simple.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.setTitle("FXML is Simple");
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(NewFXbuilder.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
Application.launch(NewFXbuilder.class, (java.lang.String[])null);
}
}
My simple.fxml,simplecclass.java and NewFXbuilder.java all resides in the same folder javafxapplication2.
while running NewFXBuilder.java, but it gives me the following error.
javafxapplication2.NewFXbuilder start
SEVERE: null
javafx.fxml.LoadException: java.lang.ClassNotFoundException: simplecclass
javafxapplication2.NewFXbuilder start SEVERE: null
javafx.fxml.LoadException: java.lang.ClassNotFoundException:
simplecclass
Looks like a problem in the FXML file. Make sure you import simplecclass in the FXML file.
The mistake I did was forgotten to add java packagename in the controller class name field in scene builder. It should have been packagename.simplecclass but I gave simplecclass alone,which is a mistake.

Categories