I am fairly new to the JavaFX side of things but so far I have managed to build a Java Web Browser that takes in local html files, however the html files I have also contain links to pdfs. I read that you can use getHostServices() to open up the default pdf application on the system, but I keep getting null pointer exceptions. Everything is imported correctly.
public class AbsorbBrowser extends Region{
public Application a;
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
public AbsorbBrowser(){
webEngine.load(getClass().getResource("/resources/test.html").toExternalForm());
getChildren().add(browser);
browser.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
String str = webEngine.getLocation();
String newStr = str.substring(8);
a.getHostServices().showDocument(newStr);
// hostServices.showDocument(file.getAbsolutePath());
}
});
I have it so that when the mouse is clicked that it will assign whatever that location (URL) was to a string, that string would lead to the location:
file:///C:/Users/Me/Desktop/Absorb/bin/resources/test.pdf
So I take a substring and remove the first 8 characters to leave it just with the C:/ directory, then pass that string into the getHostServices().showDocument();
Null Pointer Exception
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
On the line a.getHostServices().showDocument(newStr);
I tried using java File, but to still no avail. I would appreciate any help or recommendations, thanks.