gpt4 book ai didi

java - 按金额删除购物车中的产品

转载 作者:行者123 更新时间:2023-12-01 14:57:02 25 4
gpt4 key购买 nike

我有一个注册应用程序,我必须在购物篮中添加删除选项。 Basktet

现在我将按钮设置为循环,以便为每个产品创建一个按钮。这一切都很好,但每当我将多个相同的元素放入购物篮时,它也会将其删除,而不是减少数量。基本上我希望它像: cars aantal: 3 ,当我单击该产品的删除按钮时,aantal 是 2 直到其为 1,然后删除该项目。我知道这是通过设置标签文本来完成的,但标签数量与产品数量相关联。这是我的代码:

付款类别:

for (int i = 0; i < products.size(); i++) {
Product product = products.get(i);
productdelete = product;

lblamount = new JLabel(String.valueOf(WinkelApplication.getBasket().getProductAmount(productdelete)));
lblamount.setBounds(410, verticalPosition + i * productOffset, 70, 20);
lblamount.setFont(WinkelApplication.FONT_10_PLAIN);
add(lblamount);

DeleteProduct deleteproduct = new DeleteProduct();

JButton btnDeleteproduct = new JButton("X");
btnDeleteproduct.setBounds(380, verticalPosition + i * productOffset, 20, 20);
btnDeleteproduct.setFont(WinkelApplication.FONT_10_BOLD);
btnDeleteproduct.setName("" + i);
btnDeleteproduct.addActionListener(deleteproduct);
add(btnDeleteproduct);
}

删除产品类别:

public void productlist(){
List<Product> products = WinkelApplication.getBasket().getProducts();

for(int i = 0; i < products.size(); i++){
deleteproduct = products.get(i);
}
}

public void actionPerformed(ActionEvent event){
productlist();

productamount = WinkelApplication.getBasket().getProductAmount(deleteproduct);

if(productamount == 1){
WinkelApplication.getBasket().deleteProduct(deleteproduct);
}else if (productamount > 1){
productamount--;
WinkelApplication.getBasket().setProductAmount(productamount);
WinkelApplication.getInstance().showPanel(new view.Payment());
}

}

每当我将此代码放在其他地方加载时:

productamount = WinkelApplication.getBasket().getProductAmount(deleteproduct);

它给了我一个错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at model.Basket.getProductAmount(Basket.java:62)
at view.DeleteProduct.productlist(DeleteProduct.java:27)
at view.DeleteProduct.actionPerformed(DeleteProduct.java:34)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6382)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6147)
at java.awt.Container.processEvent(Container.java:2083)
at java.awt.Component.dispatchEventImpl(Component.java:4744)
at java.awt.Container.dispatchEventImpl(Container.java:2141)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
at java.awt.Container.dispatchEventImpl(Container.java:2127)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4572)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:704)
at java.awt.EventQueue.access$400(EventQueue.java:82)
at java.awt.EventQueue$2.run(EventQueue.java:663)
at java.awt.EventQueue$2.run(EventQueue.java:661)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:677)
at java.awt.EventQueue$3.run(EventQueue.java:675)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:674)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

最佳答案

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

清楚地表明您尝试获取的对象为 null 尝试进行 null 检查 if(WinkelApplication.getBasket()!=null)

关于java - 按金额删除购物车中的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14233029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com