我正在尝试使用MVC原理使用自定义类“ Media”的ArrayList填充JTable,但是当我调用其在模型中创建列表的方法时,我正在从View类中获取NullPointerException。应该使用数据库中的数据填充此列表,尽管我在实现DB之前正在使用测试数据。
我尝试在不同的地方实例化ArrayList。如果我在模型中执行此操作(应该如何完成)并将该ArrayList返回给控制器,则可以从控制器打印项目,但将其传递给View会引发异常。如果我在控制器内部创建ArrayList,则我的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 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)
我希望能够将在我的Model上实例化的ArrayList传递给Controller,然后传递给View,而不必将其另存为类变量到Controller上并为其创建一个getter(这是唯一可能的方法)我能想到的解决方案)。
答案 0 :(得分:1)
您正在向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);
}