尝试使用Media Player时Application Constructor中的异常

时间:2016-04-19 17:30:35

标签: java javafx media-player

我已经编写了一个代码,可以在找到网页上的链接时播放音乐。

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javafx.application.*;
// * @author Archit

public abstract class WebCrawl extends Application{

    public static void main(String[] args) throws IOException {
        Application.launch(args);
        int a=0;
        try {
            Document doc = Jsoup.connect("https://in.bookmyshow.com/ranchi").get();
            org.jsoup.select.Elements links = doc.select("a");
            for (Element e: links) {
                if ((e.attr("abs:href").equals("https://in.bookmyshow.com/ranchi/movies/fan/ET00025074"))) {
                    try {
                        File f = new File("/Users/Archit/Documents/Music/campbell.wav");
                        Media hit = new Media(f.toURI().toString());
                        MediaPlayer mediaPlayer = new MediaPlayer(hit);
                        mediaPlayer.play();
                    } catch(Exception ex) {
                        System.out.println("Exception");
                    }
                }
            }
        }

我得到的错误是:

Exception in Application constructor 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: Unable to construct Application   instance: class webcrawl.WebCrawl
    at   com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:819)
    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 webcrawl.WebCrawl

当我运行应用程序时,似乎会打开一个窗口但是会自动关闭并出现此错误。

我真的很感激帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

您正在使用WebCrawlApplication.launch(String[])班级开始申请,因此launch会尝试创建一个WebCrawl个实例,因为{ {1}}是WebCrawl

BTW:在abstract调用之后放置代码无法正常工作,因为在Application.launch完成之后,JavaFX平台已经退出。

您可以在Life-cycle section of the Application javadoc

中了解应用程序生命周期

您需要使用Application.launch方法或更高版本调用代码。

您可以在此处找到简单JavaFX应用程序的教程:https://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm