0

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;
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    `shopIterator` doesn't look like it's been initialised, but, from our perceptive, neither could `table` or `list1` or `cart` – MadProgrammer Aug 16 '21 at 10:05
  • 1
    `if(a.getName() == value)` -> Relevant: [How do I compare Strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – maloomeister Aug 16 '21 at 10:06
  • Going off of your code comment "_//HERE IS THE ERROR SHOWN_", then `cart` has not been initialized. – maloomeister Aug 16 '21 at 10:08
  • I edited the Post, the shopIterator and cart are initialized in the top of the GUI as it should. So is the table (it woulndt show me that value = plushie in the console log if it woulndt work)..I am really lost, I bet its some small error XD – user16600500 Aug 16 '21 at 10:20
  • "_I edited the Post, the shopIterator and cart are initialized..._" - actually, we can only tell that `cart` and `shopIterator` are in fact **not** initialized. You only showed us `private Cart cart;` and `private Iterator shopIterator;`, which means they are both _declared_ but not _initialized_. – maloomeister Aug 16 '21 at 10:24
  • Initializing would mean to assign a value to it. The table for example is filled with products from the database, so it has those values there. As for the shopIterator, I dont quiet understand how to initialize it..but if it woulndt work, woulndt it show me an error there already? I thought maybe its a problem with the database, since some of the values of the product are shown in the console as NULL, as shown..even when i declare them manually – user16600500 Aug 16 '21 at 10:32
  • OKay it works when i put the Cart c = new Cart() in the method...but I dont think thats a good idea, right? I heard that I shoulndt do that, since I cant use it anywhere else then – user16600500 Aug 16 '21 at 10:39
  • *"since I cant use it anywhere else then"* That's true if an instance is declared & created in a method and *dispose* at the end of the method (or nothing else is done with it). But if the newly created instance is returned from the method, or assigned to a class level attribute or (added to a) collection before the method ends, it will persist. – Andrew Thompson Aug 16 '21 at 11:50

0 Answers0