-1

I've got a problem with a NullPointerException.

This is my Controller:

    public class Controller implements Initializable{

    public Label lbTitle;
    public Label lbAuthor;
    public Label lbChapters;
    public TextField tfTitle;
    public TextField tfAuthor;
    public TextArea taChapters;
    public ListView<Book> listView;

    private void OpenNewWindow(Book book1) throws Exception {

        String selected = listView.getSelectionModel().getSelectedItem().toString();

        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(getClass().getResource("/zli/BooksFX/NewWindow.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 400, 400);
        Stage stage = new Stage();

        tfTitle.setText(book1.getTitle());
        tfAuthor.setText(book1.getAuthor());
        taChapters.setText(book1.toStringChapters());

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

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Chapter chapter1Book1 = new Chapter(1, 100, "First Chapter...");
        Chapter chapter2Book1 = new Chapter(2, 400, "Second Chapter...");
        ArrayList<Chapter> book1Chapters = new ArrayList<>();
        book1Chapters.add(chapter1Book1);
        book1Chapters.add(chapter2Book1);
        Book book1 = new Book("IT: Not the one with the clown", "Stephen ITBerg", book1Chapters);

        Chapter chapter1Book2 = new Chapter(1, 100, "First Chapter...");
        Chapter chapter2Book2 = new Chapter(2, 400, "Second Chapter...");
        ArrayList<Chapter> book2Chapters = new ArrayList<>();
        book1Chapters.add(chapter1Book2);
        book1Chapters.add(chapter2Book2);
        Book book2 = new Book("Z Men", "Stan Lee", book2Chapters);

        Chapter chapter1Book3 = new Chapter(1, 100, "First Chapter...");
        Chapter chapter2Book3 = new Chapter(2, 400, "Second Chapter...");
        ArrayList<Chapter> book3Chapters = new ArrayList<>();
        book1Chapters.add(chapter1Book3);
        book1Chapters.add(chapter2Book3);
        Book book3 = new Book("Captain AlivePool", "Undead Zombie", book3Chapters);

        ObservableList<Book> items = FXCollections.observableArrayList (book1, book2, book3);
        listView.setItems(items);
        listView.setOnMouseClicked(e -> {
            try {
                OpenNewWindow(book1);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        });

    }
}

And this is my .fxml:

<Pane prefHeight="400.0" prefWidth="400.0" style="-fx-background-color: #1ba77f;" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="zli.BooksFX.Controller">
    <ListView fx:id="listView" disable="true" opacity="0.0" prefHeight="400.0" prefWidth="400.0" />
   <Label fx:id="lbTitle" layoutX="8.0" layoutY="20.0" prefHeight="27.0" prefWidth="57.0" text="Title: " />
   <Label fx:id="lbAuthor" layoutX="8.0" layoutY="90.0" text="Author: " />
   <TextField text="ert" fx:id="tfTitle" editable="false" layoutX="72.0" layoutY="14.0" prefHeight="39.0" prefWidth="316.0" />
   <TextField text="sdf" fx:id="tfAuthor" editable="false" layoutX="72.0" layoutY="84.0" prefHeight="39.0" prefWidth="316.0" />
   <Label fx:id="lbChapters" layoutX="8.0" layoutY="160.0" text="Chapters: " />
   <TextArea text="sdfs" fx:id="taChapters" editable="false" layoutX="7.0" layoutY="187.0" prefHeight="200.0" prefWidth="383.0" />
</Pane>

And this is the Error message:

java.lang.NullPointerException
    at zli.BooksFX.Controller.OpenNewWindow(Controller.java:37)
    at zli.BooksFX.Controller.lambda$initialize$0(Controller.java:73)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3589)
    at javafx.graphics/javafx.scene.Scene$ClickGenerator.access$8300(Scene.java:3517)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3885)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Scene.java:3604)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2613)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
    at java.base/java.lang.Thread.run(Thread.java:844)

Now, when I want to set the Text of my TextFields and TextAreas (tfTitle, tfAuthor, taChapters) to the assigned value, it gives me a "java.lang.NullPointerException" on the Line 37 in Controller, which is this:

tfTitle.setText(book1.getTitle());

And for your information: "Book" is a storage Class for Chapters.

Because it was asked for, my other .fxml:

<GridPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"
          fx:controller="zli.BooksFX.Controller">
<ListView fx:id="listView" prefWidth="400" />
</GridPane>
NicoB
  • 75
  • 9

2 Answers2

1

May be you have not initialized your Texfield. use @FXML Annotation over tfTitle declaration

@FXML
public TextField tfTitle;

Same goes for other fields too.

  • This didn't work, still the same Error message. – NicoB Mar 22 '18 at 12:05
  • Have you used scene builder for design ? So far I can recall there is a way to generate controller from that directly. This way you can avoid this issue. Beaides, print your book1 before use to make sure that is not null. – Nazar-E-Bukhari Mar 22 '18 at 12:20
  • I already printed what book1.getTitle() contains. It does return me the expected String, which I want. So I think, the NullPointerException goes for the TextField. :/ – NicoB Mar 22 '18 at 12:30
  • The `@FXML` is only necessary when you have a non-public field. For more see: [Is @FXML needed for every declaration?](https://stackoverflow.com/q/30210170/8097737) –  Mar 22 '18 at 13:10
0

You use the class Controller as the controller of two different fxml-files.

The first only declares listView but not tfTitle, tfAuthor and taChapters therefore you get a NullPointerException at tfTitle.setText(book1.getTitle());

When you load the second fxml-file in OpenNewWindow you also creates a new Controller object.

Now you can reuse the first controller with fxmlLoader.setController(this); and remove fx:controller="zli.BooksFX.Controller" from NewWindow.fxml (Don't do this since you will run into similar problems like this).

Or create two independent controller for each fxml-file and pass the book between the controllers.

For the second controller you need something like:

public void setBook(Book book) {
    tfTitle.setText(book.getTitle());
    tfAuthor.setText(book.getAuthor());
    taChapters.setText(book.toStringChapters());
}

And pass the book with ((BookDetailController)fxmlLoader.getController()).setBook(book1); after fxmlLoader.load()