JavaFX NullPointerException Location is required NetBeans [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 2 years ago.
i've tried anything else i found on stack stackoverflow and i really dont get it why this doesn't work.
I won't show you the code of my application that is not working, because it isn't working even with the example project.
So here is the problem:
When i create new JavaFX Application with the sample code that gives button which prints hello world after clicked, this works when i run this as a desktop application and when i build this and start in browser. This works perfectly as desktop and as browser application
But when i create new JavaFX FXML Application which is almost the same as above but stage is defined by fxml and css not byte he code. This one works perfectly as windows application but doesnt work as a browser application
java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:22)
at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/15592694.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/19532686.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$35/9825943.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.NullPointerException: Location is required.
at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/15592694.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/19532686.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$35/9825943.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafxapplication3.JavaFXApplication3.start(JavaFXApplication3.java:22)
... 11 more
here is the code of working application:
public class JavaFXApplication4 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
and this application doesnt work in browser and throws exception:
public class JavaFXApplication3 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);
}
}
Can someone help me?

The problem is in this line:
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
It is pretty clear that the load method is actually being called with a null argument. And that happens because getResource("/sample/sample.fxml") cannot file that resource.
The resource is missing (or has the wrong path) on the runtime classpath.
Source

The problem has to do with signing but I do not exactly understand what that means. Then I searched for how to run a JavaFX application in a browser with netbeans and found this: http://docs.oracle.com/javafx/2/fxml_get_started/fxml_deployment.htm
In netbeans 8.2 in the project properties under Build > Deployment there is a check-box:
Request unrestricted access (Enable signing)
With this it works. Good luck.

In some case, if you using maven you should move all your fxml files to the resources directory and use a relative address from there.
If you want to use stage.setScene() function inside a controller class, it's better to use:
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);

if u are using different packages then cut that file from other package and paste it in main package. And then try to run . I have tried that approach and its working fine now .

Related

How can I fix "javax.swing.ImageIcon.<init>(Unknown Source)"?

I'm making a simple program of a PONG game, and my problem comes from the Main method which i wanted to set an image icon and it shoots me a NullPointerException.
I'm working with Eclipse IDE 2019,06 and the Java Compiler 12.0.1. The image that I am using is .jpg.
I've tried to change the directory of the image and nothing :(.
The directory of the image is in a folder which is in the same folder of the main class.
This is my frame code:
public class Menu {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Menu window = new Menu();
window.frame.setVisible(true);
window.frame.setLocationRelativeTo(null);
window.frame.setTitle("PONG!");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Menu() {
frame.setIconImage(new ImageIcon(Menu.class.getResource("/icon.jpg")).getImage());
initialize();
}
So this is the error message:
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at gui.Menu.<init>(Menu.java:57)
at gui.Menu$1.run(Menu.java:42)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I hope you can help me with this problem. Thanks.
I guess line 57 of Menu.java is this line:
frame.setIconImage(new ImageIcon(Menu.class.getResource("/icon.jpg")).getImage());
It looks like frame has not been initialized and therefore is null. That's why you get a NullPointerException.
When you work with the Maven build system (e.g. netbeans 11.3, maven 3.3.x), you have to take care where you place your resources. The "/icon.jpg" path is relative to your resources directory. Try to create a directory named "resources" (sic!) as a subdirectory of the java main source directory. For example, the path to "resources" should be
<Project root>/src/main/resources
You place your image files in this directory or a maybe a subdirectory /src/main/resources/icons/icon.jpg

Null pointer exception on passing a value between 2 controllers

Introduction
I am learning Java and JavaFX and in order to do that I am working on a little project which objective is to behave as password generator.
Basically, I have 2 windows, the first one allows the user to chose what kind of password he wants. Once he is done with that, the password is generated and it should be displayed on the second window.
What is particular here is that I decided to try out the procedural and declarative for coding the windows. Which means that the windows generating the password is coded in a java file. A contrario, the window that displays the password is declared in a FXML file.
What I struggle to do is to pass the generated password to the second window. I tried many things (bad things like using static methods) and I thought about trying to use bindings (that I only recently discovered).
But this last option didn't help either as I still get the same error all the time : a null pointer exception. It is coming from the line where the password is generated by the model and the obtained String is bound to a value in the controller of the seconde view.
I am kind of stuck here and I am thinking that mixing up 2 different ways to code my views isn't the best method. Still, maybe am I not doing the binding correctly, that's what I think and hope the most.
The code
So the controller of my first view looks like this (generating the password) :
public class GeneratePasswordController implements EventHandler<MouseEvent>{
#FXML private displayPasswordController displayPasswordController;
#Override
public void handle(MouseEvent event) {
//Doing some stuff that works, then generating the password and null pointer exception occurs here
//The method getNewPassword() returns a String (the password).
//The model is accessed statically (an instance has been created in the Application file (Main.java)).
displayPasswordController.pwdValueProperty().bind(Bindings.createStringBinding(()
-> Main.myModel.getNewPassword()));
}
}
And the controller of the view that displays the password :
public class NewPswdController {
#FXML private TextField displayPassword;
private final StringProperty pwdValue = new SimpleStringProperty("Password");
public StringProperty pwdValueProperty() {
return pwdValue;
}
public String getPwdValue() {
return pwdValue.get();
}
public void setPwdValue(String value) {
this.pwdValue.set(value);
}
#FXML
void initialize() {
dispPassword.textProperty().bind(Bindings.format("%s", pwdValue));
}
}
The null pointer exception appears at the specific line in the controller of the view that generates the password where the model actually generates it.
I am giving it, I guess it should help the most, but I couldn't really use that information untill now:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at s03.GeneratePasswordController.handle(GeneratePasswordController.java:61)
at s03.GeneratePasswordController.handle(GeneratePasswordController.java:1)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$271/1952832519.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1232367853.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Thank you for looking into it. I might learn something new there I guess, but it seems that I can't find it out by myself and it is driving me nuts since a couple of days now.
If it appears that more information is needed, I can provide more. I included everything I think is necessary.
The trouble was coming from the controller that displays the password. The null pointer was most likely coming from the fact that there was no instance of that controller.
What is wrong is the part where I include the controller that displays the password using the #FXML annotation.
The following code, in the controller that generates the password, works just fine :
try {
//Load the view and controller
FXMLLoader loader = new FXMLLoader(getClass().getResource("displayPassword.fxml"));
Parent displayPassword = (Parent)loader.load();
Scene displayPasswordScene = new Scene(displayPassword);
displayPasswordController controller = loader.getController();
//Generate the password and set it
controller.setPwdValue(Pastis.model.getNewPassword());
//Load the new view on the stage
Main.getStage().setScene(displayPasswordScene);
Main.getStage().show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I found the answer on this question : FXMLLoader getController returns NULL?.

Controls FX TraversalEngine Error

I installed JDK8u20 and tried to run my program with the ControlsFX library in it.
This should work fine, but if I call a method from controlsFX, I get the following exception
Method
private void showError(final String msg) {
Platform.runLater(new Runnable() {
#Override
public void run() {
Dialogs.create().title("Achtung").message(msg).showError();
}
});
}
Exception
java.lang.NoSuchMethodError: com.sun.javafx.scene.traversal.TraversalEngine.<init>(Ljavafx/scene/Parent;Z)V
at org.controlsfx.control.ButtonBar$2.<init>(ButtonBar.java:412)
at org.controlsfx.control.ButtonBar.<init>(ButtonBar.java:412)
at org.controlsfx.control.ButtonBar.<init>(ButtonBar.java:355)
at org.controlsfx.dialog.Dialog.createButtonPanel(Dialog.java:1034)
at org.controlsfx.dialog.Dialog.createCenterPanel(Dialog.java:1029)
at org.controlsfx.dialog.Dialog.buildDialogContent(Dialog.java:950)
at org.controlsfx.dialog.Dialog.show(Dialog.java:320)
at org.controlsfx.dialog.Dialogs.showSimpleContentDialog(Dialogs.java:1106)
at org.controlsfx.dialog.Dialogs.showError(Dialogs.java:555)
at ch.berufsbildungscenter.notiztool.gui.control.LoginController$3.run(LoginController.java:93)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1171794308.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1875594551.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/994750745.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Adding an answer for someone facing the same issue in future :
ControlsFX 8.0.6 was released in two separate releases
ControlsFX 8.0.6, for developers using JavaFX 8.0
ControlsFX 8.0.6_20, for developers building with JavaFX 8u20
If you are wondering why ? here is an explanation to it
If you are using JDK8, go with ControlsFX 8.0.6
If you are using JDK8_20 or later, go with ControlsFX 8.0.6_20

JavaFX Text in/on Polygon

I wanted to take a text (javafx.scene.text) and put it on a polygon.
I tried it with a Group (javafx.scene.group) by trying this tutorial: tutorial on stackoverflow
This doesn't work with the text.setClip(Polygon).
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at ch.berufsbildungscenter.notiztool.gui.control.BbcPolygon$1.run(BbcPolygon.java:33)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Is there another way to get a Text onto a polygon?
Thanks
Peace
setClip() has different purpose. You can check the documentation To easily put Text on a Polygon or any other node, you can use javafx.scene.layout.StackPane like this: StackPane stack=new StackPane();
stack.getChildren().add(polygonInstance);
stack.getChildren().add(textInstance); The node last added will be on top.

Swing apps doesn't run

I have a problem in using swing. I don't know what is the cause of this but i'm just trying to create a simple frame app using swing at it give me a lot of error.
import javax.swing.JFrame;
public class StacksGui {
public static void main(String args[])
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(100,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Exception in thread "main" java.lang.ExceptionInInitializerError
at javax.swing.JPanel.updateUI(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JRootPane.createGlassPane(Unknown Source)
at javax.swing.JRootPane.<init>(Unknown Source)
at javax.swing.JFrame.createRootPane(Unknown Source)
at javax.swing.JFrame.frameInit(Unknown Source)
at javax.swing.JFrame.<init>(Unknown Source)
at StacksGui.main(StacksGui.java:9)
Caused by: java.lang.IllegalArgumentException: 0 incompatible with Text-specific LCD contrast key
at java.awt.RenderingHints.put(Unknown Source)
at sun.awt.windows.WDesktopProperties.getDesktopAAHints(Unknown Source)
at sun.awt.windows.WToolkit.getDesktopAAHints(Unknown Source)
at sun.awt.SunToolkit.getDesktopFontHints(Unknown Source)
at sun.awt.windows.WDesktopProperties.getProperties(Unknown Source)
at sun.awt.windows.WToolkit.updateProperties(Unknown Source)
at sun.awt.windows.WToolkit.lazilyInitWProps(Unknown Source)
at sun.awt.windows.WToolkit.lazilyLoadDesktopProperty(Unknown Source)
at java.awt.Toolkit.getDesktopProperty(Unknown Source)
at javax.swing.UIManager.<clinit>(Unknown Source)
... 10 more
I had problem when all java Swing apps crashed silently.
After trying to launch from command line got the following exception:
Caused by: java.lang.IllegalArgumentException: 181193932 incompatible with Text-specific LCD contrast key
As it's said in JDK-6503988, the problem is connected with ClearType registry setting FontSmoothingGamma.
This can be fixed by running "Adjust ClearType text" from Control panel (cttune.exe) or by changing the registry directly:
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /t REG_DWORD /v FontSmoothingGamma /d 1024 /f
A reboot may be needed for changes to take the effect.
See also a 10-year-old investigation of the same problem at https://www.rarst.net/software/cleartype-install4j-java-bug/.
This code works for me:
package test;
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(100,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I only see a difference at
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
where I added the frame. However your stacktrace looks like there's another error, so first try this and if it still don't work, give us a bit more code to look at.

Categories