Exception 调用方法时,应用程序启动方法中发生异常
我正在用javafx编写一个web浏览器。我在获取web引擎中当前打开的页面的标题时遇到问题。每当我调用getTitle方法时,在编译程序时它都会给我带来一系列错误(Exception 调用方法时,应用程序启动方法中发生异常,exception,javafx,javafx-webengine,Exception,Javafx,Javafx Webengine,我正在用javafx编写一个web浏览器。我在获取web引擎中当前打开的页面的标题时遇到问题。每当我调用getTitle方法时,在编译程序时它都会给我带来一系列错误(private final String title=getTitle(webEngine);) 我得到的错误是 Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.Nativ
private final String title=getTitle(webEngine);
)
我得到的错误是
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
应用程序启动方法中出现异常
java.lang.reflect.InvocationTargetException
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:483)
位于com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:363)
位于com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:303)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:483)
位于sun.launcher.launchelper$FXHelper.main(launchelper.java:767)
原因:java.lang.RuntimeException:应用程序启动方法中的异常
位于com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:875)
在com.sun.javafx.application.launchempl.lambda$launchApplication$147(launchempl.java:157)
位于com.sun.javafx.application.launchempl$$Lambda$53/1598924227.run(未知来源)
运行(Thread.java:745)
原因:java.lang.NullPointerException
位于com.rajat.browser.browser.getTitle(Main.java:307)
在com.rajat.browser.browser.(Main.java:77)
位于com.rajat.browser.Main.start(Main.java:53)
在com.sun.javafx.application.launchempl.lambda$launchApplication1$153(launchempl.java:821)
位于com.sun.javafx.application.LaunchImpl$$Lambda$59/857338418.run(未知源)
位于com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
位于com.sun.javafx.application.PlatformImpl$$Lambda$49/1401420256.run(未知源)
位于com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
位于com.sun.javafx.application.PlatformImpl$$Lambda$52/1424777668.run(未知来源)
位于java.security.AccessController.doPrivileged(本机方法)
com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
位于com.sun.javafx.application.PlatformImpl$$Lambda$51/752848266.run(未知来源)
位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95)
在com.sun.glass.ui.gtk.GtkApplication.\u runLoop(本机方法)
在com.sun.glass.ui.gtk.GtkApplication.lambda$null$45(GtkApplication.java:126)
位于com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/1469821799.run(未知来源)
... 还有一个
运行应用程序com.rajat.browser.Main时出现异常
Java结果:1
谁来帮我修一下这个。我将非常感谢你。你也可以检查我的项目
谢谢:)要找到问题,我们需要查看您的项目,并检查您在创建
浏览器的实例时是否调用了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()
返回null。然后是下一行的NPE
解决方案:在继续之前检查doc
是否为空
private static String getTitle(WebEngine webEngine) {
Document doc = webEngine.getDocument();
if(doc==null){
return "";
}
...
}
您可能真正想要做的是跟踪WebEngine的title属性的更改,并在标题更改时更新一些标签文本。这与我很久以前写文章时所做的相似
webView.getEngine().titleProperty().addListener((observableValue, oldValue, newTitle) -> {
if (newTitle != null && !"".equals(newTitle)) {
label.setText(newTitle);
} else {
label.setText("");
}
});
它还类似于中提供的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");
导入javafx.concurrent.Worker.State;
最后阶段;
webEngine.getLoadWorker().stateProperty().addListener(
新的ChangeListener(){
公共无效已更改(ObservalEvalue ov、State oldState、State newState){
if(newState==State.successed){
stage.setTitle(webEngine.getLocation());
}
}
});
webEngine.load(“http://javafx.com");
我已经实现了您指定的代码,但我遇到了一个奇怪的问题,有时它能够找到网页的标题,但有时它显示了我分配给newTitile变量的默认值。
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");