How to specify a controller in a GUI app? - java

Im doing a simple GUI project with java. Sometimes it notices my fxml file and can run normally, but after a while it chooses to ignore it, throwing the following message:
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$1(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: No controller specified.
/C:/Users/M%c3%a1t%c3%a9/Desktop/Workspace/KisallatNyilvantartoRendszer/target/classes/main/allatok.fxml:24
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at main.Main.start(Main.java:18)
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.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
... 1 more
Exception running application main.Main
My main looks like this(and the allatok.fxml file is located in the main package, next to the main.java):
package main;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent load = FXMLLoader.load(getClass().getResource("allatok.fxml"));
Scene value = new Scene(load);
primaryStage.setScene(value);
primaryStage.setResizable(true);
primaryStage.setTitle("Kisallat Nyilvantarto Rendszer");
primaryStage.show();
}
}
Where should I put the fxml file, and how should I rename the path?

At your top most XML tag you can specify the Fully Qualified Class Name (FQCN) using the XML attribute fx:controller
Example: AnchorPane
<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.zigma.Controller">
The code for com.zigma.Controller
package com.zigma;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
public class Controller implements Initializable{
#Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
System.out.println("controller init");
}
}

Related

UnsatisfiedLinkError occurs on openjfx webkit(32bit)

In order to use webview with amazoncorretto 8 (32bit), I want to build 32bit openjfx.
I could create a library, but when I use it I get an UnsatisfiedLinkError.
Build it with reference to the following URL.
https://dzone.com/articles/how-to-build-openjfx-8-on-windows-from-source
Visual Studio :Community ⇒ Professional
It is targeting 32 bit
Note: No error occurred when building on 64 bit.
Sample Code:
import java.util.Properties;
import java.util.TreeSet;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class SimpleJavaFX8Example extends Application {
private Stage stg;
#Override
public void start(Stage stg) throws Exception {
this.stg = stg;
stg.setTitle(getClass().getSimpleName());
VBox box = new VBox();
WebView wv = new WebView();
WebEngine engine = wv.getEngine();
StringBuilder buf = new StringBuilder();
buf.append("<table>");
Properties props = System.getProperties();
for (String name : new TreeSet<>(props.stringPropertyNames())) {
String val = props.getProperty(name);
val = val.replace("&", "&").replace("<", "<");
buf.append("<tr><td>").append(name).append("</td><td>").append(val).append("</td></tr>");
}
buf.append("</table>");
engine.loadContent("<title>t</title><h1>System Properties</h1>" + buf.toString(), "text/html");
box.getChildren().add(wv);
VBox.setVgrow(wv, Priority.ALWAYS);
stg.setScene(new Scene(box));
stg.show();
}
public static void main(String[] args) {
launch(args);
}
}
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: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:748)
Caused by: java.lang.UnsatisfiedLinkError: com.sun.webkit.WebPage.twkInitWebCore(ZZZ)V
at com.sun.webkit.WebPage.twkInitWebCore(Native Method)
at com.sun.webkit.WebPage.lambda$static$0(WebPage.java:156)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.webkit.WebPage.<clinit>(WebPage.java:132)
at javafx.scene.web.WebEngine.<init>(WebEngine.java:881)
at javafx.scene.web.WebEngine.<init>(WebEngine.java:868)
at javafx.scene.web.WebView.<init>(WebView.java:273)
at SimpleJavaFX8Example.start(SimpleJavaFX8Example.java:23)
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.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
... 1 more
Exception running application SimpleJavaFX8Example

Launching fmxl GUI using javafx on Eclipse, Error in start method

I am trying to launch a fmxl file using eclipse.
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.application.Application;
import java.io.IOException;
import java.util.logging.Logger;
import java.util.logging.Level;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
public class MainProgram extends Application {
public void start(Stage stage) {
try {
FXMLLoader fmxlLoader = new FXMLLoader();
String viewerFxml = "Interface.fmxl";
AnchorPane page = (AnchorPane)fmxlLoader.load(
this.getClass().getResource(viewerFxml).openStream());
Scene scene = new Scene(page);
stage.setScene(scene);
stage.show();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
System.exit(1);
}
}
public static void main(String[] args) {
launch(args);
}
}
However, I keep getting 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: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.NullPointerException
at MainProgram.start(MainProgram.java:20)
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)
Exception running application MainProgram
The code appears to run up until this line:
AnchorPane page = (AnchorPane)fmxlLoader.load(
this.getClass().getResource(viewerFxml).openStream());
Really not sure why I'm getting this error.
Any help would be very much appreciated.
It is because the path of file which has to be loaded is not correct.
Try putting the correct file path in your String variable.
that is
String viewerFxml= "~path/Interface.fxml";

Sending data from one tab to another tab in javafx 8 with eclipse and scene builder

I created two Tabs with Scene Builder, one fxml-file for each tab. These two files are included in an main.fxml file. In Eclipse I have one controller for each fxml file (MainController, Tab1Controller, Tab2Controller). To shorten these explanation. I want to send or load Strings one tab to the other tab via MainController (Like the mediator pattern). But i get the following error code
javafx.fxml.LoadException:
/C:/Users/.../javafxmars/Test_TabController/bin/view/Main.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:14)
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)
at java.lang.Thread.run(Unknown Source)
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)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 17 more
Caused by: java.lang.NullPointerException
at controller.MainController.initialize(MainController.java:14)
... 27 more
I searching for my mistake since a few days, but i cant find my anything. I looked for the ids a several times, but nothing. The FXML loader seems alright too.
public void start(Stage primaryStage) {
try {
new FXMLLoader();
Parent root = FXMLLoader.load(getClass().getResource("/view/Main.fxml"));
Scene scene = new Scene(root);
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);
}
The MainController looks as followed:
public class MainController {
#FXML Tab1Controller tab1controller;
#FXML Tab2Controller tab2controller;
#FXML public void initialize(){
System.out.println("Program initilized");
tab1controller.init(this);
tab2controller.init(this);
}
public String getTextloadedfromTab1() {
return tab1controller.label1.getText();
}
public void setTab2LabelText(String text) {
tab2controller.label2.setText(text);
}
}
If i have to I post the code from the two tabs or the code for the fxml files. I will be glad, if someone could help me out.
Here are the fxml documents
FXML Documents UPDATE
These project is based on these tutorial https://github.com/goranvasic/JavaFxTutorials/tree/master/JavaFxCommunicationBetweenControllers
In your main FXML you include the tabs like:
<fx:include fx:id="tab_Test1" source="tab/Tab_Test1.fxml" />
And then in your MainController you inject it like:
#FXML Tab1Controller tab1controller;
Which is not the correct way to do it. The valid name is fx:idController (fx id with a "Controller" suffix):
#FXML Tab1Controller tab_Test1Controller;
Nested Controller naming.

JavaFX FXML Slider to return integers instead of Doubles [duplicate]

I have this controller class for showing a database query in a TableView, but i am having error of NullPointerException with the "setCellValueFactory(new PropertyValueFactory"
package aplicativo;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
public class Controle implements Initializable{
#FXML
private TextField txtCampo,txtCampo2;
#FXML
private Button btAdicionar,btConsultar;
#FXML
private TableView<Pessoa> tabValues;
#FXML
private TableColumn<Pessoa, Integer> tbcCod;
private TableColumn<Pessoa, String>tbcNome;
ObservableList<Pessoa> List = FXCollections.observableArrayList();
#FXML
private void btAdd(){
insertBD a = new insertBD(txtCampo.getText());
consultaBD b = new consultaBD();
List = b.consultaTudo();
tabValues.setItems(List);
txtCampo.clear();
}
#FXML
private void btCons(){
String tx = txtCampo2.getText();
if(tx.isEmpty()){
}else{
consultaBD a = new consultaBD();
a.consultaParecido(tx, "nome");
txtCampo2.clear();
}
}
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
tbcCod.setCellValueFactory(new PropertyValueFactory<Pessoa, Integer>("cod"));
tbcNome.setCellValueFactory(new PropertyValueFactory<Pessoa, String>("nome"));
tabValues.setItems(List);
tabValues.getColumns().addAll(tbcCod,tbcNome);
}
}
The NullPointerExcepetion:
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/1645995473.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/lucas/workspace/BDFX/bin/aplicativo/Tela.fxml
at javafx.fxml.FXMLLoader.constructLoadException(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 aplicativo.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/1031257736.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1529876784.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/237061348.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/2117255219.run(Unknown Source)
... 1 more
Caused by: java.lang.NullPointerException
at aplicativo.Controle.initialize(Controle.java:52)
... 23 more
Exception running application aplicativo.Main
Any solution?
One of the instance fields in your controller is lacking an #FXML annotation. Since the field is private, the FXML loader is unable to inject the control reference into the field during loading. Here are your instance field declarations:
#FXML
private TextField txtCampo,txtCampo2;
#FXML
private Button btAdicionar,btConsultar;
#FXML
private TableView<Pessoa> tabValues;
#FXML
private TableColumn<Pessoa, Integer> tbcCod;
private TableColumn<Pessoa, String>tbcNome;
Notice that the last field, tbcNome, is not annotated. As a result, when your initialize method is called, the tbcNome field contains a null reference, resulting in the exception.
To fix your problem, all you may need to do is add the #FXML annotation to the instance field declaration for tbcNome.
You may have encouraged this error by adopting the habit of listing more than one variable in your type declarations, eg. private Button btAdicionar, btConsultar;. In my opinion, this is a bad habit that can encourage errors like this to happen. I would suggest that you try to adopt the coding style in which each instance field has its own type declaration statement.

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