java - 返回导致 NullPointerException 的 ArrayList 的方法

标签 java model-view-controller arraylist

<分区>

我正在尝试使用 MVC 原则使用自定义类“Media”的 ArrayList 来填充 JTable,但是当我的 View 类调用在模型中创建列表的方法(应该用数据库中的数据填充此列表,尽管我在实现数据库之前使用测试数据)。

我已经尝试在不同的地方实例化我的 ArrayList。如果我在模型中执行它(它应该如何完成)并将该 ArrayList 返回到我的 Controller ,我可以从 Controller 打印项目,但将它传递到我的 View 会引发异常。如果我在 Controller 内创建 ArrayList,我的 View 就可以很好地访问它。我还尝试将我对模型的引用保留为公共(public)(未经面向对象的观点建议)并尝试直接从我的 View 调用该方法以获得相同的结果。

模型类

public class SearchMediaModel {

    //Default constructor
    public SearchMediaModel() { }

    //returns an ArrayList of Media Objects (test data)
    public ArrayList<Media> getTitlesFromDB() {
        ArrayList<Media> list = new ArrayList<>();
        Media test = new TvBox("Game of Thrones");
        Media test2 = new TvBox("House of Cards");
        Media test3 = new TvBox("The Sopranos");
        list.add(test3);
        list.add(test2);
        list.add(test);
        return list;

    }

}

Controller 类

//controller to the view that will list all the titles in the DB
public class SearchMediaController {

    private SearchMediaView view;
    public SearchMediaModel model;

    //Constructor instantiates both View and Model classes and saves them in the
    //class variables
    public SearchMediaController() {
        this.view = new SearchMediaView(this);
        this.model = new SearchMediaModel();
    }


    //returns an Array List of Medias from the model
    //If I try printing the items from the list here, it works
    public ArrayList<Media> getMediaList() {
        return model.getTitlesFromDB();
    }

}

查看类

public class SearchMediaView extends JFrame{

    private SearchMediaController controller;

    public SearchMediaView(SearchMediaController controller) {
        this.controller = controller;

//exception happening in this line of code
        ArrayList<Media> listForTheTable= controller.getMediaList();

堆栈跟踪

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at searchcustomer.SearchMediaController.getMediaList(SearchMediaController.java:29)
    at searchcustomer.SearchMediaView.<init>(SearchMediaView.java:22)
    at searchcustomer.SearchMediaController.<init>(SearchMediaController.java:21)
    at frontPage.FrontPageController.actionPerformed(FrontPageController.java:95)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6397)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

我期望能够将在我的模型上实例化的这个 ArrayList 传递到我的 Controller ,然后再传递到我的 View ,而不必将它保存为我的 Controller 上的类变量并为它创建一个 getter(这是唯一的其他可能我能想到的解决方案)。

最佳答案

您正在向 SearchMediaController 构造函数传递一个 SearchMediaController 实例,该实例未完全初始化为 SearchMediaView 构造函数,它试图访问 media 初始化前的实例变量。

改变

public SearchMediaController() {
    this.view = new SearchMediaView(this);
    this.model = new SearchMediaModel();
}

public SearchMediaController() {
    this.model = new SearchMediaModel();
    this.view = new SearchMediaView(this);
}

关于java - 返回导致 NullPointerException 的 ArrayList 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55920773/

相关文章:

php - 服务之间应如何通信?

java - 有没有一种方法可以更新 Arraylist 中的记录而不删除现有记录?

java - 在 Apache Kafka 中读取消息偏移量

javascript - 从 MVC View 访问 DLL 中的 SaveMethod

java - 如果已知 UTC 的日期和该时区的时间,如何知道该时区的日期?

grails - 什么是全栈MVC框架? Grails如何是全栈框架?

java - mockito ArrayList<String> 问题

java - 是否可以使用 for 循环来创建单选按钮?

java - 使用 JavaFX 的 WebView NullPointerException

java - 衍生线程中的 Spring 事务