我试图使用使用MVC原则的自定义类"Media“的ArrayList来填充一个ArrayList,但是当它调用在模型中创建列表的方法时,它会从我的View类中得到一个NullPointerException (虽然在实现DB之前我正在使用测试数据)来填充这个列表。
我尝试在不同的地方实例化我的ArrayList。如果我在模型中这样做(应该如何完成)并将该ArrayList返回给我的控制器,我可以从控制器中打印条目,但是将它传递给我的视图会抛出异常。如果我在控制器内创建ArrayList,我的视图就可以访问它了。我还尝试将对模型的引用保留为公共的(不建议使用和面向对象的透视图),并试图从我的视图直接调用该方法以获得相同的结果。
模型类
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)我本来希望能够将这个在我的模型上实例化的ArrayList传递给我的控制器,然后传递给我的视图,而不必将它保存为我的Controller上的类变量,并为它创建一个getter (这是我能想到的唯一其他可能的解决方案)。
发布于 2019-04-30 12:19:29
您正在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);
}https://stackoverflow.com/questions/55920773
复制相似问题