title: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException GUI Java/ Eclipse -> Add Product to Cart
body: <p>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:</p>
  <pre><code>public void actionPerformed(ActionEvent e) {
                  int column = 0;
                  int row = table.getSelectedRow();
                  int Id;
                  int number = 0;
          
                  if (row == -1) {
                      showMessageDialog(null, &quot;No Product was selected&quot;);
                  }
                  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)
                      }
                  }
              }
              }
                  
              
          });
  </code></pre>
  <p>This is the Error message I am getting, when trying to put the product in the cart:</p>
  <pre><code>Exception in thread &quot;AWT-EventQueue-0&quot; 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)
  </code></pre>
  <p>I not quiet understand, since my try in the Main Method with an example worked very well:</p>
  <pre><code>     public static void main(String[] args){
  
              Product pdk = new Product(1, &quot;Test&quot;, 2f, &quot;Testing&quot;, 12, &quot;something.png&quot;, 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']
  
          }
  }
  </code></pre>
  <p>If my cart model is needed, let me know.</p>
  <p>-&gt; Implemented in Eclipse</p>
  <p>cart is initialized in the top of the GUI as:</p>
  <pre><code>import common.cart.Cart;
  private Cart cart;
  private ArrayList&lt;Product&gt; list = new ArrayList&lt;&gt;();
  private Iterator&lt;Product&gt; shopIterator;
  </code></pre>