JavaFX Invocation Target Exception - Illegal State Exception target not set - java

My main class can't seem to find my fmxl file it seems but I'm pretty sure the pathing is correct. I don't understand what is happening.
Here is my main
public void start(Stage primaryStageObj) throws Exception{
primaryStage = primaryStageObj;
// System.out.println(getClass().getResource("/net/usikkert/kouchat/practice.fxml"));
FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/net/usikkert/kouchat/practice.fxml"));
Parent root2 = loader2.load();
System.out.println("controller set");
LoginController loginController = loader2.getController();
loginController.setArgumentParser(argumentParser);
loginController.setSettings(settings);
loginController.setUncaughtExceptionLogger(uncaughtExceptionLogger);
primaryStage.setTitle("Flake");
primaryStage.setScene(new Scene(root2, 250,439 ));
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("Perfect Version.png")));
primaryStage.show();
primaryStage.setResizable(false);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent e) {
Platform.exit();
System.exit( 0);
}
});
primaryStage.setTitle("Flake");
The error I am receiving is
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at net.usikkert.kouchat.Main.start(Main.java:74)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Exception running application net.usikkert.kouchat.Main
The error at Main.java:74 is my loader call Parent root2 = loader2.load();
I don't understand what the error is caused by is my fxml file not able to be found?
That is the path of my project

Change:
FXMLLoader loader2 = new FXMLLoader(getClass().getResource("/net/usikkert/kouchat/practice.fxml"));
to
FXMLLoader loader2 = new FXMLLoader(getClass().getResource("practice.fxml"));

Related

JavaFX : Exception in thread "main"

I'm using JavaFX and when I run with java application I have this error. The jar file of javaFX is included in libraries of the project but but I don't know what this exception means.I'm using Ubuntu 19.04.
I do the following :
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
and the output is:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: java.lang.NoSuchMethodError: <init>
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:267)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:211)
at com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:675)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:337)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
... 5 more
Caused by: java.lang.NoSuchMethodError: <init>
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
at java.lang.Runtime.load0(Runtime.java:809)
at java.lang.System.load(System.java:1086)
at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:107)
at com.sun.glass.utils.NativeLibLoader.loadLibrary(NativeLibLoader.java:39)
at com.sun.glass.ui.gtk.GtkApplication.lambda$new$203(GtkApplication.java:151)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.<init>(GtkApplication.java:140)
at com.sun.glass.ui.gtk.GtkPlatformFactory.createApplication(GtkPlatformFactory.java:41)
at com.sun.glass.ui.Application.run(Application.java:146)
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:257)
... 9 more

Project compiles in intellij but not when exported

So my application runs and functions normally when I run it through intellij, but when I export the application it wont even launch. This is the Stack Trace I get.
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at application.UIController.start(UIController.java:42)
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 application.UIController
Process finished with exit code 1
This is the code that it is complaining about.
public void start(Stage stage) {
screen = stage;
try {
FXMLLoader loader = new FXMLLoader(UIController.class.getResource("/fxml/LoginFXML.fxml"));
Parent root = loader.load();
loginController = loader.getController();
Scene scene = new Scene(root, 340, 487);
scene.getStylesheets().add(getClass().getResource("/css/application.css").toExternalForm());
screen.setScene(scene);
screen.show();
screen.setResizable(false);
screen.setTitle("Room Booking System");
if (launched.equals(false)) {
launched = true;
Database_Control launchdb = new Database_Control();
launchdb.launch();
}
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
screen.setX((screenBounds.getWidth() - screen.getWidth()) / 2);
screen.setY((screenBounds.getHeight() - screen.getHeight()) / 2);
} catch (IOException e) {
e.printStackTrace();
}
}
It complains about a location not being set, so I tried
FXMLLoader loader = new FXMLLoader(); loader.setLocation(UIController.class.getResource("/fxml/LoginFXML.fxml"));
However it just kept giving me the same problems. Thanks in advance.
My File Structure
https://gyazo.com/cec056fb358f453d32f49676bde12b5d
Location not set errors mean that the compiler could not find the FXML file you're referring to. I do see your FXML file is loginFXML.fxml but you're trying to load the file with a capital L: LoginFXML.fxml. This doesn't seem to have any effect in testing, but perhaps something happens with that when exporting?

JavaFX can't load stylesheets: rootPane.getStylesheets().add(url.toString()); causing NullPointerException

I'm trying to load a CSS stylesheet with the following code in my controller's initialize method:
public class Controller implements Initializable {
#FXML BorderPane rootPane;
#FXML TextField txtTest;
#FXML Button btnSayHey;
#FXML Button btnLookMa;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
String styleFile = "res/striped-progress.css";
URL url = getClass().getResource(styleFile);
rootPane.getStylesheets().add(url.toString());
}
}
however upon launching the app, there's a javafx.fxml.LoadException being thrown and looking at the stacktrace, I can see that
rootPane.getStylesheets().add(url.toString());
is causing a NullPointerException. My start method is as follows:
#Override
public void start(Stage primaryStage) throws Exception {
String sceneFile = "scripts/DEV/PLAYGROUND/STYLES/res/MainView.fxml";
Parent root = null;
URL url = null;
try
{
url = new File(sceneFile).toURI().toURL();
root = FXMLLoader.load( url );
System.out.println( " fxmlResource = " + sceneFile );
}
catch ( Exception ex )
{
System.out.println( "Exception on FXMLLoader.load()" );
System.out.println( " * url: " + url );
System.out.println( " * " + ex );
System.out.println( " ----------------------------------------\n" );
throw ex;
}
BorderPane page = (BorderPane) root;
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.setTitle("SampleCSS App - v1.0");
primaryStage.show();
}
And console stacktrace is here:
Exception on FXMLLoader.load()
* url: file:/C:/rangedb/workspaces/UKRangeDBdemo/app_RVSW/scripts/DEV/PLAYGROUND/STYLES/res/MainView.fxml
* javafx.fxml.LoadException:
/C:/rangedb/workspaces/UKRangeDBdemo/app_RVSW/scripts/DEV/PLAYGROUND/STYLES/res/MainView.fxml
----------------------------------------
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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:483)
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:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$49/1545327692.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
/C:/rangedb/workspaces/UKRangeDBdemo/app_RVSW/scripts/DEV/PLAYGROUND/STYLES/res/MainView.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3208)
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 DEV.PLAYGROUND.STYLES.ExternalStylesSample.start(ExternalStylesSample.java:79)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$52/897256917.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/661672156.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1517660793.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$47/128893786.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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/1349277854.run(Unknown Source)
... 1 more
Caused by: java.lang.NullPointerException
at DEV.PLAYGROUND.STYLES.Controller.initialize(Controller.java:49)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2542)
... 22 more
Exception running application DEV.PLAYGROUND.STYLES.ExternalStylesSample
You'll notice that simply commenting out/deleting the line
rootPane.getStylesheets().add(url.toString()); in initialize() will cause all the errors to go away, but this will not load the CSS stylesheet.
Update:
Adding package structure under James_D's advice :
(I've managed to fix my own problem thanks to James_D's comments. So here goes... )
The NullPointerException was being caused by me not initialising the BorderPane object in my MainView.fxml file. After setting the fx:id of the BorderPane in the fxml file as rootPane (note: this name was set to rootPane because in my Controller, I have a #FXML injected field:
#FXML Parent rootPane;
after making this change, almost all errors were gone. Except the following runtime warning:
com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged
INFO: Could not find stylesheet: file:/C:/rangedb/workspaces/UKRangeDBdemo/app_RVSW/res/striped-progress.css
Albeit the CSS was being applied to my controls during runtime. Turns out one can load the stylesheet in a 'privileged' manner by simply using toExternalForm() on the URL object that was being returned by:
getClass().getResource("res/striped-progress.css")
In other words, I changed the above line of code to:
rootPane.getStylesheets().add(getClass().getResource("res/striped-progress.css").toExternalForm());
toExternalForm() returns a String representation of the URL object but I'm not sure why not using it causes a warning and using it doesn't. Either way, it works.

JavaFX MediaPlayer throws java.lang.reflect.InvocationTargetException

I've been trying to experiment with the JavaFX MediaPlayer class, and found the following example code in a different thread:
import java.net.URI;
import java.net.URISyntaxException;
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 test extends Application {
#Override
public void start(Stage primaryStage)
{
//Add a scene
Group root = new Group();
Scene scene = new Scene(root, 500, 200);
URI file = null;
try {
file = new URI("/home/thomas/voodoo.mp3");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("POOR URI SYNTAX");
}
Media pick = new Media(file.toString());
MediaPlayer player = new MediaPlayer(pick);
player.play();
//Add a mediaView, to display the media. Its necessary !
//This mediaView is added to a Pane
MediaView mediaView = new MediaView(player);
((Group)scene.getRoot()).getChildren().add(mediaView);
//show the stage
primaryStage.setTitle("Media Player");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I adjusted the file path to point to a real audio file (/home/thomas/voodoo.mp3) and tried running it, but the console output is the following:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == '/home/thomas/voodoo.mp3'
at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:211)
at javafx.scene.media.Media.<init>(Media.java:393)
at test.start(test.java:28)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
... 1 more
Exception running application test
Is there something wrong with my setup, or with my code?
Thanks for the help!
EDIT:
Changed to Media pick = new Media(new File("/home/thomas/voodoo.mp3").toURI().toString()); as per Nash's suggestion, but it still doesn't work. The console output is a little different now, though.
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: MediaException: UNKNOWN : com.sun.media.jfxmedia.MediaException: Could not create player! : com.sun.media.jfxmedia.MediaException: Could not create player!
at javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:146)
at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:511)
at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:414)
at test.start(test.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
... 1 more
Caused by: com.sun.media.jfxmedia.MediaException: Could not create player!
at com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:274)
at com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:118)
at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:467)
... 11 more
Exception running application test
The problem is your url /home/thomas/voodoo.mp3 that is not valid, indeed the scheme is missing as described into your error message (uri.getScheme() == null! uri == '/home/thomas/voodoo.mp3'), it should start with file:// as it is a file in your local file system such that it should be file:///home/thomas/voodoo.mp3.
But since it is too error prone to build the URI as you currently do because you must ensure that it is properly URL encoded (spaces should be for example replaced with %20) and you need to provide a valid scheme, you had better to use new File(path).toURI().toString() as proposed by Nash.
Try
Media pick = new Media(new File("/home/thomas/voodoo.mp3").toURI().toString());

MediaPlayer in JavaFX

I am concerned with the fact that JavaFX has suffered some modifications since the day of this tutorial: http://www.youtube.com/watch?v=bWl98dhvf8Q .
Please, give me some hints for what should I change in this code, or is this kind of code valid, at all? Thank you.
Here's the code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.Group;
public class BiPlayer extends Application {
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Group videoPane = new Group();
Media media = new Media("C:\\Users\\Insanovation\\Downloads\\P.mp4");
MediaPlayer player = new MediaPlayer(media);
MediaView view = new MediaView(player);
videoPane.getChildren().add(view);
Scene scene = new Scene (videoPane, 400, 400, Color.BLACK);
primaryStage.setScene(scene);
primaryStage.show();
player.play();
}
}
Here's the output:
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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:483)
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:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/1394438858.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\Insanovation\Downloads\F.mp3
at javafx.scene.media.Media.<init>(Media.java:383)
at biplayer.BiPLAYER.start(BiPLAYER.java:24)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/270894642.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$44/1147985808.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1822121612.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$45/1267032364.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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/636718812.run(Unknown Source)
... 1 more
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\Insanovation\Downloads\F.mp3
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)
at java.net.URI$Parser.parse(URI.java:3058)
at java.net.URI.<init>(URI.java:588)
at javafx.scene.media.Media.<init>(Media.java:381)
... 15 more
Exception running application biplayer.BiPLAYER
Java Result: 1
Looking at the exception that you've copied and pasted, it's clear that the exception thrown is a URISyntaxException. It then lists the problem character, which is at index 2 (the backslash character).
If you look into the documentation of URIs here and here, you'll see that only forward slashes are acceptable in URIs. If you changed the backslashes in your path to forward slashes like so:
file:C:/Users/Insanovation/Downloads/F.mp4
I believe it should work.

Categories