I`m trying to populate a JTable using an ArrayList of a custom class "Media" using MVC principles, but I'm getting a NullPointerException from my View class when it calls the method that creates the list in the model (which is supposed to populate this list with data from a database, although I'm using testing data before implementing the DB).
I have tried instantiating my ArrayList in different places. If I do it inside the model (how it's supposed to be done) and return that ArrayList to my controller, I can print the items from the controller, but passing it to my View throws the exception. If I create the ArrayList inside the controller, my View can access it just fine. I have also trying leaving my reference to the Model as public (unadvised from and Object Oriented Perspective) and tried to call the method directly from my View to the same results.
Model Class
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 Class
//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();
}
}
View Class
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();
Stack trace
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)
I was expecting to be able to pass this ArrayList instantiated on my Model to my Controller and then to my View without having to save it as a class variable on my Controller and creating a getter for it (which is the only other possible solution I could think of).