在fxml中使用Textfield的NullPointerException [重复]

时间:2021-08-19 13:58:00

This question already has an answer here:

这个问题在这里已有答案:

I've got a problem with a NullPointerException.

我遇到了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:

这是我的.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:

现在,当我想将TextFields和TextAreas的文本(tfTitle,tfAuthor,taChapters)设置为指定的值时,它在Controller的第37行上给出了一个“java.lang.NullPointerException”,它是这样的:

tfTitle.setText(book1.getTitle());

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

并且为了您的信息:“Book”是章节的存储类。

Because it was asked for, my other .fxml:

因为它被要求,我的另一个.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>

2 个解决方案

#1


0  

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

您使用类Controller作为两个不同fxml文件的控制器。

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

第一个只声明listView而不是tfTitle,tfAuthor和taChapters因此你在tfTitle.setText(book1.getTitle())得到一个NullPointerException;

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

在OpenNewWindow中加载第二个fxml文件时,还会创建一个新的Controller对象。

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).

现在你可以使用fxmlLoader.setController(this)重用第一个控制器;并从NewWindow.fxml中删除fx:controller =“zli.BooksFX.Controller”(不要这样做,因为你会遇到类似这样的问题)。

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

或者为每个fxml文件创建两个独立的控制器,并在控制器之间传递该书。

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()

并用((BookDetailController)fxmlLoader.getController())传递这本书.setBook(book1);在fxmlLoader.load()之后

#2


1  

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

可能是你没有初始化你的Texfield。在tfTitle声明上使用@FXML Annotation

@FXML
public TextField tfTitle;

Same goes for other fields too.

其他领域也是如此。

#1


0  

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

您使用类Controller作为两个不同fxml文件的控制器。

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

第一个只声明listView而不是tfTitle,tfAuthor和taChapters因此你在tfTitle.setText(book1.getTitle())得到一个NullPointerException;

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

在OpenNewWindow中加载第二个fxml文件时,还会创建一个新的Controller对象。

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).

现在你可以使用fxmlLoader.setController(this)重用第一个控制器;并从NewWindow.fxml中删除fx:controller =“zli.BooksFX.Controller”(不要这样做,因为你会遇到类似这样的问题)。

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

或者为每个fxml文件创建两个独立的控制器,并在控制器之间传递该书。

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()

并用((BookDetailController)fxmlLoader.getController())传递这本书.setBook(book1);在fxmlLoader.load()之后

#2


1  

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

可能是你没有初始化你的Texfield。在tfTitle声明上使用@FXML Annotation

@FXML
public TextField tfTitle;

Same goes for other fields too.

其他领域也是如此。