I am trying to get a Product into the Cart in the GUI element.We have a GUI with a table in which all the Products are listed. This is the method I implemented for this:
public void actionPerformed(ActionEvent e) {
int column = 0;
int row = table.getSelectedRow();
int Id;
int number = 0;
if (row == -1) {
showMessageDialog(null, "No Product was selected");
}
else {
String value = table.getModel().getValueAt(row, column +1).toString();
while(shopIterator.hasNext()) {
Product a = shopIterator.next();
System.out.println(a.toString());
//console log : id: 'null', name: 'plushie', price: '2.5' , categorie: 'plushies', image: 'null', value: 'null'
if(a.getName() == value) {
//Here I tried to make a new Product, since cart.addToCart(a) woulndt work already
Product p = new Product(a.getID(), a.getName(), a.getPrice(), a.getCategory(), a.getAvailable(),a.getImage(), 0);
list1.add(p);
System.out.println(p.toString());
//console log : id: 'null', name: 'plushie', price: '2.5' , categorie: 'plushies', image: 'null', value: 'null'
cart.addToCart(list1.get(0)); //HERE IS THE ERROR SHOWN
//Doesnt work even when only doing cart.add(a) or (p)
}
}
}
}
});
This is the Error message I am getting, when trying to put the product in the cart:
Exception in thread "AWT-EventQueue-0" id: 'null', name: 'plushie', price: '2.5' , category: 'plushies', bild: 'null', value: 'null'
java.lang.NullPointerException
at eshop.GUI.GUIProductListing$3.actionPerformed(GUIProductListing.java:150)
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:6636)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6401)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
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:2762)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
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(AccessController.java:391)
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(AccessController.java:391)
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 not quiet understand, since my try in the Main Method with an example worked very well:
public static void main(String[] args){
Product pdk = new Product(1, "Test", 2f, "Testing", 12, "something.png", 2);
Cart wkb = new Cart();
wkb.addToCart(pdk);
System.out.println(wkb.getProducts().toString());
//Console log:[id: 'null', name: 'Test', price: '2.0' , category: 'Testing', Image: 'null', Value: 'null']
}
}
If my cart model is needed, let me know.
-> Implemented in Eclipse
cart is initialized in the top of the GUI as:
import common.cart.Cart;
private Cart cart;
private ArrayList<Product> list = new ArrayList<>();
private Iterator<Product> shopIterator;