在调用Method时发生Application start方法的异常

时间:2015-01-20 17:13:42

标签: exception javafx javafx-webengine

我在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$launchApplication$147(LauncherImpl.java:157)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/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$launchApplication1$153(LauncherImpl.java:821)
    at com.sun.javafx.application.LauncherImpl$$Lambda$59/857338418.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/1401420256.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
    at com.sun.javafx.application.PlatformImpl$$Lambda$52/1424777668.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$51/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$45(GtkApplication.java:126)
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/1469821799.run(Unknown Source)
    ... 1 more
Exception running application com.rajat.browser.Main
Java Result: 1

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

2 个答案:

答案 0 :(得分:1)

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

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()返回null。然后NPE在下一行。

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

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

答案 1 :(得分:1)

您可能真正想要做的是跟踪WebEngine的title属性以进行更改,并在标题更改时更新一些标签文本。这与我很久以前写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用法代码段,该代码段监视文档属性而不是title属性:

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");