JavaFX Rectangel Array does'nt work [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
im working on a problem for like 2 hours and I still cant fix it.
My task is to programm 49 Rectangel Squares.
so I tried to use a array to make it easier, but it doen'st want to work :'(
without using arrays it works..
Thank you for help!
source code:
package Game;
import javafx.scene.shape.Rectangle;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Game extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
VBox vb = new VBox();
VBox vb2 = new VBox();
HBox hb = new HBox();
Rectangle q[] = new Rectangle[4];
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
vb.getChildren().addAll(q[0],q[1]);
vb2.getChildren().addAll(q[2],q[3]);
hb.getChildren().addAll(vb,vb2);
Scene sz1 = new Scene(hb);
primaryStage.setScene(sz1);
primaryStage.show();
}
}
my compiler :
> 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)
atcom.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Launcher
Impl.java:389)
atcom.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.jav
a: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
atcom.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.ja
va:917)
atcom.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Launc
herImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at Spiel.FensterSpiel.start(FensterSpiel.java:29)
atcom.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Laun
cherImpl.java:863)
atcom.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl
.java:326)
atcom.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:
295)
at java.security.AccessController.doPrivileged(Native Method)
atcom.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.j
ava:294)
atcom.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.ja
va:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
atcom.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:19
1)
... 1 more

You are getting a NullPointerException because you initialize an array, but forget to initialize the object in the array! q[0]-q[3] are all null. You cannot call the setFill() method on a null object.
change
Rectangle q[] = new Rectangle[4];
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
to something like
Rectangle q[] = new Rectangle[4];
// initialize the rectangles
q[0] = new Rectangle();
q[1] = new Rectangle();
q[2] = new Rectangle();
q[3] = new Rectangle();
q[0].setFill(Color.RED);
q[1].setFill(Color.BLUE);
q[2].setFill(Color.GREEN);
q[3].setFill(Color.YELLOW);
Hope this helps.

Related

How to load video in java FX

I want to insert video into javaFX. Insert video from computer - Not from youtube or something.
Play/pause/minimize buttons, borders are not needed.
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
public class MediaMP4 extends Application {
Stage window;
Scene scene1;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws FileNotFoundException {
window = primaryStage;
primaryStage.setTitle("Moves");
Media media = new Media ("pr.mp4");
MediaPlayer player = new MediaPlayer (media);
MediaView view = new MediaView (player);
Group full = new Group ();
full.getChildren().addAll(view);
scene1 = new Scene (full,600,600);
primaryStage.setScene(scene1);
window.show();
player.play();
}
}
My pr.mp4 file is inside project, not in package.
Stack trace:
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.IllegalArgumentException: uri.getScheme() == null! uri == 'pr.mp4'
at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:211)
at javafx.scene.media.Media.<init>(Media.java:393)
at vv.MediaMP4.start(MediaMP4.java:27)
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 vv.MediaMP4
The relevant line in your stack trace is:
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'pr.mp4'.
You need to specify an URI string with a scheme as a construtor argument, as specified here.
So change your line from this:
Media media = new Media ("pr.mp4");
to something like this:
Media media = new Media("file://c:/myproject/pr.mp4"));
Have a look at this question for more details: How to target a file (a path to it) in Java/JavaFX

JavaFX Clock widget throws Exceptions randomly

I have written a little Clock Widget extending the Text class of JavaFX. To update the Time I am using a Task that basically sets the text to the current System Time. When I run this application in Eclipse it sometimes throws a NullpointerException in the line I call stage.show().
This is what my widgets sourcecode looks like:
package clock;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javafx.concurrent.Task;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class CurrentClockText extends Text {
private final DateTimeFormatter formatter;
public static final int HOUR = 1;
public static final int HOUR_MINUTE = 2;
public static final int HOUR_MINUTE_SECOND = 3;
public static final int HOUR_MINUTE_SECOND_MILLISECOND = 4;
private final long updateInterval;
private final Task<Void> updater = new Task<Void>() {
#Override
protected Void call() throws Exception {
while (true) {
LocalDateTime now = LocalDateTime.now();
String nowTextual = formatter.format(now);
setText(nowTextual);
try {
Thread.sleep(updateInterval);
} catch (InterruptedException ignore) {
}
}
}
};
public CurrentClockText() {
this(HOUR_MINUTE);
}
public CurrentClockText(final int detailLevel) {
String timeFormat = "";
switch (detailLevel) {
case HOUR:
timeFormat = "HH";
updateInterval = 60000;
break;
case HOUR_MINUTE:
timeFormat = "HH:mm";
updateInterval = 15000;
break;
case HOUR_MINUTE_SECOND:
timeFormat = "HH:mm:ss";
updateInterval = 500;
break;
case HOUR_MINUTE_SECOND_MILLISECOND:
updateInterval = 1;
timeFormat = "HH:mm:ss.S";
break;
default:
throw new IllegalArgumentException(
"Unknown detail level for Clock: " + detailLevel);
}
setFont(new Font("Verdana", 28));
formatter = DateTimeFormatter.ofPattern(timeFormat);
Thread updaterThread = new Thread(updater, "CurrentClockText.updaterThread");
updaterThread.setDaemon(true);
updaterThread.start();
}
}
This is the main class:
package clock;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Boot extends Application {
#Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
CurrentClockText clock = new CurrentClockText(4);
pane.setCenter(clock);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Boot.launch(args);
}
}
And this is the stacktrace:
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(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
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(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/14845382.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at com.sun.javafx.text.PrismTextLayout.addTextRun(Unknown Source)
at com.sun.javafx.text.GlyphLayout.addTextRun(Unknown Source)
at com.sun.javafx.text.GlyphLayout.breakRuns(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.buildRuns(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.layout(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.ensureLayout(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.getBounds(Unknown Source)
at javafx.scene.text.Text.getLogicalBounds(Unknown Source)
at javafx.scene.text.Text.impl_computeLayoutBounds(Unknown Source)
at javafx.scene.Node$12.computeBounds(Unknown Source)
at javafx.scene.Node$LazyBoundsProperty.get(Unknown Source)
at javafx.scene.Node$LazyBoundsProperty.get(Unknown Source)
at javafx.scene.Node.getLayoutBounds(Unknown Source)
at javafx.scene.Node.prefWidth(Unknown Source)
at javafx.scene.Node.minWidth(Unknown Source)
at javafx.scene.layout.Region.computeChildPrefAreaWidth(Unknown Source)
at javafx.scene.layout.BorderPane.getAreaWidth(Unknown Source)
at javafx.scene.layout.BorderPane.computePrefWidth(Unknown Source)
at javafx.scene.Parent.prefWidth(Unknown Source)
at javafx.scene.layout.Region.prefWidth(Unknown Source)
at javafx.scene.Scene.getPreferredWidth(Unknown Source)
at javafx.scene.Scene.resizeRootToPreferredSize(Unknown Source)
at javafx.scene.Scene.preferredSize(Unknown Source)
at javafx.scene.Scene.impl_preferredSize(Unknown Source)
at javafx.stage.Window$9.invalidated(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.set(Unknown Source)
at javafx.stage.Window.setShowing(Unknown Source)
at javafx.stage.Window.show(Unknown Source)
at javafx.stage.Stage.show(Unknown Source)
at clock.Boot.start(Boot.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/19600960.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/18503843.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/27167109.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$47/2180324.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$36/3326003.run(Unknown Source)
... 1 more
Exception running application clock.Boot
As I said this Exception only occurs in some runs and not always. It happens mostly when I change the formatting pattern of detailLevel = 4 but remains for another run after changing it back again. I am guessing this might actually have something to do with Eclipse but I can't debug the code either because the Exception gets thrown in the stage.show call which I absolutely do not understand. What's the cause of this random Exception and how can I fix it?
In JavaFX, as in most other GUI toolkits, there is one specific thread which handles all UI related operations. An application must not update the UI outside of this thread. If the UI needs to be updated from another thread, there are usually APIs available which ensure that code is executed in the context of the UI thread. In case of JavaFX, see the Concurrency in JavaFX Tutorial for more information.
In your case, the simplest solution would be to make sure that your setText() call is executed on the JavaFX application thread, not on the thread associated with your Task:
...
Platform.runLater(() -> setText(nowTextual));
...
There are also other APIs available in JavaFX to do animations, which can be used to call a handler method at specific time intervals - that would remove the Thread.sleep() call from your loop. See Timeline for more information.

Unable to find fxml file in Javafx

I'm currently trying to follow a Javafx Tutorial on the web and I have some issues.
I have a project with 3 different packages.
Fist one is : ch.makery.address, it contains the Main
Second is : ch.makery.model it's empty for the moment
Third is : ch.makery.view it contains 2 different fxml files wich correspond to two different layouts.
Here is the Main code :
`package ch.makery.address;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
#Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("AddressApp");
initRootLayout();
showPersonOverview();
}
/**
* Initializes the root layout.
*/
public void initRootLayout() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.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();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Shows the person overview inside the root layout.
*/
public void showPersonOverview() {
try {
// Load person overview.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/PersonOverview.fxml"));
AnchorPane personOverview = (AnchorPane) loader.load();
// Set person overview into the center of root layout.
rootLayout.setCenter(personOverview);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
* #return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}}`
And here are the errors that it returns :
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(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
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(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at ch.makery.adress.MainApp.initRootLayout(MainApp.java:35)
at ch.makery.adress.MainApp.start(MainApp.java:22)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(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$149(Unknown Source)
... 1 more
Exception running application ch.makery.adress.MainApp
For what I understand of it, it appears to be a problem when locating the fxml files. But I don't understand why.
If anybody can help me on it, it would be perfect
Thanks
(Sorry if there is any mistakes in my English it's not my mother tongue)
If your Main is in ch.makery.address and your fxml in ch.makery.view then this is wrong:
view/RootLayout.fxml
as it tries to load the file from ch.makery.address.view.
Try
../view/RootLayout.fxml
instead. (Same for PersonOverview)
It is the path to the fxml files that is wrong in that tutorial had the same issue. Using the ../ to move up one directory should work to fix paths to the fxml files.

JavaFx adding images

I keep getting this error no matter what I do to change it. I am very new to JavaFX and thought I would try out a small program. I am just trying to have a picture show up on the scene but I can't even have the image load up on my program. I placed the images in a package(seaapp.images) that is next to the main package(seaapp)
package seaapp;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SeaApp extends Application {
#Override
public void start(Stage primaryStage) {
ImageView image = new ImageView(new Image(SeaApp.class.getResourceAsStream("images/space.png")));
Pane root = new Pane();
root.getChildren().add(image);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This gives me this error:
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$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/355629945.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1109)
at javafx.scene.image.Image.<init>(Image.java:694)
at seaapp.SeaApp.start(SeaApp.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/1175241703.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1685538367.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/435396101.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/485815673.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1673605040.run(Unknown Source)
... 1 more
Exception running application seaapp.SeaApp
Java Result: 1
This works fine when I add a URL of a picture as the new Image but when I have a downloaded picture, it gives me this error. My netbeans is fully updated too. I have put links to screenshots of how I set up the files. Am I missing something?
Setup of Projects:
https://drive.google.com/file/d/0B_KZLduvxt26dE5XS1p5blE5VFE/view?usp=sharing
Setup of Files:
https://drive.google.com/file/d/0B_KZLduvxt26Nm1sZWROSlRMeUE/view?usp=sharing
Link to Entire Project:
https://drive.google.com/file/d/0B_KZLduvxt26dXVFYlFhREFGZW8/view?usp=sharing
Add a forward slash / before images/space.png and your code will work fine (That is, if the path is correct).
ImageView image = new ImageView(new Image(SeaApp.class.getResourceAsStream("/images/space.png")));
EDIT:
Its supposed to work fine.
Do it this way:
Image image = new Image( getClass().getResource( "images/space.png").toExternalForm());
ImageView image = new ImageView( image);
If that doesn't work, find out which path to look:
System.out.println( "Path: " + getClass().getResource("/").toExternalForm());
System.out.println( "Path: " + getClass().getResource("images").toExternalForm());
...
and check if the image is there.
change following line :
ImageView image = new ImageView(new Image(SeaApp.class.getResourceAsStream("images/space.png")))
to
ImageView image = new ImageView(new Image(SeaApp.class.getResourceAsStream("file:./"+"images/space.png")))
reference : https://stackoverflow.com/a/23878479/6336264
Had the same problem with image inside jar wrapped into exe
The problem was the first capital letter in the file name. Paths are case-sensitive in wrapper but not when you run it under IDE on Windows.

JavaFX transition scene from Controller

I am very new to JavaFX (and java in general) and I have been trying to transition between scenes from an FXML controller. I have tried to look up multiple solutions online however, none of them seem to work.
My main java code:
package main;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene1 = new Scene(root);
primaryStage.setScene(scene1);
primaryStage.setTitle("Login");
primaryStage.show();
}
}
... and my LoginController:
package main;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class LoginController implements Initializable
{
#FXML
private Label loginLabel;
#FXML
private TextField fieldUsername;
#FXML
private PasswordField fieldPassword;
#FXML
public void loginEvent(ActionEvent event) throws Exception{
//This is where I try to change the scene
if (fieldUsername.getText().equals("admin") && fieldPassword.getText().equals("admin")){
Parent parent = FXMLLoader.load(getClass().getResource("Main.fxml"));
Stage primaryStage = new Stage();
Scene scene = new Scene (parent);
primaryStage.setScene(scene);
primaryStage.setTitle("Main Frame");
primaryStage.show();
}
else {
loginLabel.setText("Incorrect Username or Password");
}
}
#Override
public void initialize(URL arg0, ResourceBundle arg1) {}
}
Here is the Error I get:
Caused by: 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 sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 48 more
Caused by: javafx.fxml.LoadException:
/F:/Programming/JAVA/Eclipse/Password%204/bin/main/Main.fxml:9
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.access$700(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(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.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 main.LoginController.loginEvent(LoginController.java:34)
... 57 more
Caused by: java.lang.ClassNotFoundException: main.MainController
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 71 more
My way of changing the scene doesn't work. How would I go about this issue?
Thanks in advance.
What is wrong with your solution
Your getResource argument is wrong - you should not use a file path (e.g. F:/) instead you should use something related to your class path.
You may have other errors, I haven't checked, just wanted to note that obvious one.
How to fix it
Easiest solution is to place the Main.fxml in the same directory as LoginController.java and check that, when you compile the program, Main.fxml has been copied by your build system to the same directory as LoginController.class.
For your lookup just use FXMLLoader.load(getClass().getResource("Main.fxml")); (similarly for your Login.fxml).
Sample code
Here is a sample for switching FXML based scenes (your code could be simpler if you want to replace the scene content as a whole rather than parts of the scene like the sample does).

Categories

Resources