Reputation: 487
I am coding a web browser in javafx. I am facing a problem in getting the title of the page currently open in the web engine. It gives me a bunch of errors while compiling the programe Whenever i call getTitle method(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 ;
}
The Error which i get is
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
Somebody please help me in fixing this. I would be very thankfull to you. You can also check my project here Thank you :)
Upvotes: 0
Views: 2052
Reputation: 159376
What you probably really want to do is to track the title property of the WebEngine for changes and update some label text whenever the title changes. This is similar to what I did when I wrote a web browser for JavaFX a long time ago.
webView.getEngine().titleProperty().addListener((observableValue, oldValue, newTitle) -> {
if (newTitle != null && !"".equals(newTitle)) {
label.setText(newTitle);
} else {
label.setText("");
}
});
It is also similar to the WebView usage code snippet provided in the WebEngine javadoc, that snippet monitors the document property rather than the title property:
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");
Upvotes: 1
Reputation: 45456
To find the problem, we need to look at your project, and check that you have a call to getTitle()
when you create an instance of 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");
...
}
}
Evidently, at this point, webEngine
is empty, and webEngine.getDocument()
returns null. Then the NPE at the next line.
Solution: Check if doc
is null before going any further
private static String getTitle(WebEngine webEngine) {
Document doc = webEngine.getDocument();
if(doc==null){
return "";
}
...
}
Upvotes: 1