0

When I try to run my program the WebViewer doesn't show a Webpage, it is just white like it's not loading. I tried a lot of tutorials and nothing seems to work for me. It should be a simple program: type in a book title and show the wikibooks site referring to this book. Here's my code:

Main

public class Main extends Application {

    public TextField tfSearch;
    public Button btnSearch;
    public WebView webView;

    @Override
    public void start(Stage stage) throws IOException {


        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("wikibooks.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 1000, 550);

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        WikiBooksController wbc = new WikiBooksController();
        wbc.initialize();
        launch();
    }
}

Wikibooks Controller

public class WikiBooksController {
    @FXML
    private TextField tfSearch;
    private Button btnSearch;
    private WebView webView;
    private VBox vBox;

    @FXML
    public void initialize() {
        WebEngine engine = webView.getEngine();
        engine.load("http://www.wikibooks.org");
    }
}

What am I doing wrong?

Error Message when I try to run it:
Gtk-Message: 21:00:00.529: Failed to load module "canberra-gtk-module"
Gtk-Message: 21:00:00.529: Failed to load module "canberra-gtk-module"
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.web.WebView.getEngine()" because "this.webView" is null
    at de.ab5.abgabe5/de.ab5.abgabe5.WikiBooksController.initialize(WikiBooksController.java:22)
    at de.ab5.abgabe5/de.ab5.abgabe5.Main.main(Main.java:33)
    ... 11 more
Exception running application de.ab5.abgabe5.Main
Malte
  • 11
  • 1
  • In the code that you pasted there is no initialization of the 'webView' field. – Mirek Pluta Dec 01 '21 at 20:24
  • 1
    Also, you should not instantiate the controller yourself and should not call `initialize()` on it. All of that is done for you by the `FXMLLoader`. – James_D Dec 01 '21 at 20:32

0 Answers0