调用方法时应用程序启动方法发生异常

Exception in Application start method occurs while calling Method

我正在用 javafx 编写 Web 浏览器。我在获取当前在 Web 引擎中打开的页面的标题时遇到问题。每当我调用 getTitle 方法(private final String title = getTitle(webEngine);)

// to get title of page currently open in web engine
private static String getTitle(WebEngine webEngine) {    
Document doc = webEngine.getDocument();
NodeList heads = doc.getElementsByTagName("head");
String titleText = webEngine.getLocation() ; // use location if page does not define a title
if (heads.getLength() > 0) {
    Element head = (Element)heads.item(0);
    NodeList titles = head.getElementsByTagName("title");
    if (titles.getLength() > 0) {
        Node title = titles.item(0);
        titleText = title.getTextContent();            
    }
}
return titleText ;  
}

我得到的错误是

    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$launchApplication7(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda/1598924227.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at com.rajat.browser.Browser.getTitle(Main.java:307)
    at com.rajat.browser.Browser.<init>(Main.java:77)
    at com.rajat.browser.Main.start(Main.java:53)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda/857338418.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1401420256.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1424777668.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:291)
    at com.sun.javafx.application.PlatformImpl$$Lambda/752848266.run(Unknown Source)
    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(GtkApplication.java:126)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda/1469821799.run(Unknown Source)
    ... 1 more
Exception running application com.rajat.browser.Main
Java Result: 1

有人请帮我解决这个问题。我会非常感谢你。你也可以查看我的项目here 谢谢:)

要找到问题,我们需要查看您的项目,并检查您在创建 Browser 的实例时是否调用了 getTitle():

class Browser extends Region{  

    private final  String        title      = getTitle(webEngine);

    private static String getTitle(WebEngine webEngine) {    
        Document doc = webEngine.getDocument();
        NodeList heads = doc.getElementsByTagName("head");
        ...
    }
}

显然,此时webEngine为空,webEngine.getDocument()returns为空。然后是下一行的 NPE。

解决方案:在继续之前检查 doc 是否为 null

private static String getTitle(WebEngine webEngine) {    
    Document doc = webEngine.getDocument();
    if(doc==null){
        return "";
    }
    ...
}

您可能真正想要做的是跟踪 WebEngine 的标题 属性 的更改,并在标题更改时更新一些标签文本。这类似于我很久以前写一个web browser for JavaFX时所做的事情。

webView.getEngine().titleProperty().addListener((observableValue, oldValue, newTitle) -> {
    if (newTitle != null && !"".equals(newTitle)) {
        label.setText(newTitle);
    } else {
        label.setText("");
    }
});

也类似于WebEngine javadoc中提供的WebView使用代码片段,该片段监控文档属性而不是标题属性:

import javafx.concurrent.Worker.State;
final Stage stage;
webEngine.getLoadWorker().stateProperty().addListener(
        new ChangeListener<State>() {
            public void changed(ObservableValue ov, State oldState, State newState) {
                if (newState == State.SUCCEEDED) {
                    stage.setTitle(webEngine.getLocation());
                }
            }
        });
webEngine.load("http://javafx.com");