0

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.

rmackenzie
  • 31
  • 6
  • You don't seem to initialize `a` anywhere (which would explain the null pointer exception). – James_D Apr 24 '16 at 15:27
  • Hi James, thanks for the response, would you mind letting me know what would I initialize it to? I can't say "null" as it throws back the null pointer again. I have checked different ways of doing things online but I can't seem to find any helpful resource. Thanks. – rmackenzie Apr 24 '16 at 15:50
  • You probably need to pass a reference to your application instance (or just the `HostServices` instance) to your `AbsorbBrowser` instance, but you haven't really given enough information about how you are using this to provide an answer to that. – James_D Apr 24 '16 at 15:51
  • Hi James, thanks for your help I have it working now. – rmackenzie Apr 24 '16 at 16:04

0 Answers0