简体   繁体   English

JavaFX:运行application.main的异常

[英]JavaFX: Exception running application.main

I'm fairly new to java and been learning GUI stuff using JavaFX. 我是java的新手,并且正在使用JavaFX学习GUI。 I'm trying to code a MediaPlayer. 我正在尝试编写MediaPlayer代码。 My files are as follows:- 我的文件如下: -

Main.java:- Main.java:-

package application;

import javafx.application.Application;
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Parent;



public class Main extends Application {

@Override
public void start(Stage primaryStage) {
    try {/*
UPDATED: Parent root= FXMLLoader.load(getClass().getResource("/home/User001/Templates/Java-Projects/musicPlayer/src/application/Sample.fxml"));
*/
        Parent root= FXMLLoader.load(getClass().getResource("Sample.fxml"));
        Scene scene= new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }catch(IOException e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) {
    launch(args);
}
}

SampleController.java:- SampleController.java:-

package application;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.FileChooser;

public class SampleController implements Initializable {

private String filePath;
@FXML
private MediaPlayer mp;
@FXML
private MediaView mv;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {}

@FXML
public void handleButtonAction(ActionEvent event) {
    FileChooser fc= new FileChooser();
    FileChooser.ExtensionFilter extensionFilter= new FileChooser.ExtensionFilter("Video files with mp4 extension only.", "*.mp4");
    fc.getExtensionFilters().add(extensionFilter);

    File file= fc.showOpenDialog(null);
    filePath= file.toURI().toString();
    if(filePath!= null) {
        Media media= new Media(filePath);
        mp= new MediaPlayer(media);
        mv.setMediaPlayer(mp);
        DoubleProperty width= mv.fitWidthProperty();
        DoubleProperty height= mv.fitHeightProperty();
        width.bind(Bindings.selectDouble(mv.sceneProperty(), "width"));
        height.bind(Bindings.selectDouble(mv.sceneProperty(), "height"));
        mp.play();
    }
}

@FXML public void playButton(ActionEvent event) {mp.play(); mp.setRate(1);}
@FXML public void pauseButton(ActionEvent event) {mp.pause();}
@FXML public void stopButton(ActionEvent event) {mp.stop();}
@FXML public void forwardButton(ActionEvent event) {}
@FXML public void rewindButton(ActionEvent event) {}
@FXML public void fasterButton(ActionEvent event) {mp.setRate(2);}
@FXML public void slowerButton(ActionEvent event) {mp.setRate(0.5);}
@FXML public void exitButton(ActionEvent event) {System.exit(0);}
}

Sample.fxml Sample.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.BorderPane?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.media.MediaView?>

<BorderPane maxHeight= "-Infinity" maxWidth= "-Infinity" minHeight= "-Infinity" minWidth= "-Infinity" prefHeight= "400.0" prefWidth= "600.0" xmlns= "http://javafx.com/javafx.com/javafx/8.0.141" xmlns:fx= "http://javafx.com/fxml/1" fx:controller="application.SampleController">
<bottom>
<VBox prefHeight= "29.0" prefWidth= "600.0" BorderPane.alignment="CENTER">
<children>
<HBox alignment= "TOP_CENTER" prefHeight= "100.0" prefWidth= "200.0">
<children>
<Button mnemonicParsing= "false" onAction= "#handleButtonAction" text= "Open">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
<Button mnemonicParsing= "false" onAction="#playButton" text= "Play">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
<Button mnemonicParsing= "false" onAction= "#pauseButton" text= "Pause" />
<Button mnemonicParsing= "false" onAction= "#stopButton" text= "Stop" />
<Button mnemonicParsing= "false" onAction= "#forwardButton" text= "Forward" />
<Button mnemonicParsing= "false" onAction= "#pauseButton" text= "Rewind" />
<Button mnemonicParsing= "false" onAction= "#fasterButton" text= "2X" />
<Button mnemonicParsing= "false" onAction= "#slowerButton" text= "0.5X" />
<Button contentDisplay= "CENTER" mnemonicParsing= "false" onAction= "#exitButton" text= "Exit" textAlignment= "Center" />
</children>
</HBox>
</children>
</VBox>
</bottom>
<center>
<MediaView fx:id= "mv" fitHeight= "300.0" fitWidth= "500.0" BorderPane.alignment= "CENTER" />
</center>
</BorderPane>

Now, when i try to run my Main.java it returns the following warnings and errors. 现在,当我尝试运行我的Main.java时,它返回以下警告和错误。 EDIT[1]-UPDATED STACK 编辑[1] -UPDATED STACK

Some details i'd like to share 我想分享一些细节

Prism-ES2 Error : GL_VERSION (major.minor) = 1.4
javafx.fxml.LoadException: 
/home/User00/Templates/Java-Projects/musicPlayer/bin/application/Sample.fxml:32

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
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:17)
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)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:510)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:258)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
at javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:512)
at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:363)
at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:325)
at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:767)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
... 17 more
Caused by: 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.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:506)
... 26 more
Caused by: java.lang.IllegalArgumentException: No enum constant javafx.scene.text.TextAlignment.Center
at java.lang.Enum.valueOf(Enum.java:238)
at javafx.scene.text.TextAlignment.valueOf(TextAlignment.java:32)
... 36 more
  • Openjdk version-- "1.8.0_161" Openjdk版本 - “1.8.0_161”
  • I'm using EclipseIDE With JavaFX extension installed. 我正在使用EclipseIDE With JavaFX扩展。

I'd like to know what's causing these Exceptions? 我想知道造成这些例外的原因是什么? Specially what location has java.NullPointerException been asking for? 特别是java.NullPointerException要求的位置是什么? And how can i prevent these mistakes in future? 我怎样才能防止将来出现这些错误?

Well the StackTrace for JavaFX is a bit huge so you should know how to read it: 那么StackTrace for JavaFX有点大,所以你应该知道如何阅读它:

First of all look for the last Caused by : 首先寻找最后Caused by

Caused by: java.lang.NullPointerException: Location is required.

Then search for your code and the line above it: 然后搜索您的代码及其上方的行:

at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:17)

With this information you know that faulty line is: 有了这些信息,您就知道错误的行是:

FXMLLoader.load(getClass().getResource("/home/User001/Templates/Java-Projects/musicPlayer/src/application/Sample.fxml"));

Or that your getClass().getResource(...) just returns null since the path is invalid. 或者你的getClass().getResource(...)只返回null,因为路径无效。 You need to provide path a relative to your class-file for getResource() 你需要为getResource()提供一个相对于你的类文件的路径

In your case getClass().getResource("Sample.fxml") should be enough. 在你的情况下, getClass().getResource("Sample.fxml")就足够了。


Update: 更新:

java.lang.IllegalArgumentException: No enum constant javafx.scene.text.TextAlignment.Center

And in this case the first line is quite useful: 在这种情况下,第一行非常有用:

javafx.fxml.LoadException: 
/home/User00/Templates/Java-Projects/musicPlayer/bin/application/Sample.fxml:32

Search for Center case-sensitive or the line number 32 in Sample.fxml and you find: 在Sample.fxml中搜索Center区分大小写或行号32,您会发现:

<Button contentDisplay= "CENTER" mnemonicParsing= "false" onAction= "#exitButton" text= "Exit" textAlignment= "Center" />

Change Center to CENTER : 改变CenterCENTER

<Button contentDisplay= "CENTER" mnemonicParsing= "false" onAction= "#exitButton" text= "Exit" textAlignment= "CENTER" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:无法找到或加载主类 application.Main JAVAFX - Error: Could not find or load main class application.Main JAVAFX 错误:无法找到或加载主类 application.Main 导致:java.lang.NoClassDefFoundError:javafx/application/Application JDK 11 - Error: Could not find or load main class application.Main Caused by: java.lang.NoClassDefFoundError: javafx/application/Application JDK 11 JavaFx 问题。 运行应用程序 lms.main.mainLoader 的异常 - JavaFx problem. Exception running application lms.main.mainLoader 错误:无法找到或加载主类 application.Main - Error: Could not find or load main class application.Main 致命异常主要在Android上运行的应用程序? - Fatal Exception Main running Application on Android? 线程“main”中的 JavaFX 异常 java.lang.NoClassDefFoundError: javafx/application/Application - JavaFX Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application 当后台线程运行时,如何在JavaFX应用程序中测试main方法? - How to test the main method in a JavaFX application when a background thread is running? 为ARM运行JavaFX应用程序 - Running JavaFX application for ARM 应用程序中的异常-Javafx - Exception in Application - Javafx 线程“JavaFX 应用程序线程”中的 JavaFx 异常 线程“主”java.lang.NoClassDefFoundError 中的异常:org/apache/log4j/Logger - JavaFx Exception in thread "JavaFX Application Thread" Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM