gpt4 book ai didi

java.lang.IllegalArgumentException : no product found

转载 作者:行者123 更新时间:2023-12-01 09:35:28 30 4
gpt4 key购买 nike

嗨,在学习 spring 时,我陷入了以下代码,不确定这段代码有什么问题

@Repositiry

 @Repository
public class ProductRepositiryImpl implements ProductRepositiry {

private List<Product> listOfAllProducts = null;

public ProductRepositiryImpl() {
listOfAllProducts = new LinkedList<>();

Product iphone = new Product("A1232", "iPhone", new BigDecimal(500));
iphone.setDescription("iPhone 6s with 5.2' FHD Screen");
iphone.setManfactuer("Apple");
iphone.setCategory("Smart Phone");
iphone.setUnitsInStock(699);

listOfAllProducts.add(iphone);
}

@Override
public List<Product> getAllProducts() {
return listOfAllProducts;
}

@Override
public Product getProductsById(String productId) {
Product productById = null;
for (Product product : listOfAllProducts) {
if (product != null && product.getProductId() != null && product.getProductId().equals(productId)) {
productById = product;
break;
}

if (productById == null)
throw new IllegalArgumentException("no product found " + productId);

}
return productById;

}

}


**Service**



@Service
public class ProductServiceImpl implements ProductService {

@Autowired
ProductRepositiry productRepositiry;

@Override
public void processOrder(String productId, long count) {
System.out.println("inside Process Order");

Product getProductByid = productRepositiry.getProductsById(productId);
System.out.println("get product by id");
System.out.println(getProductByid);
if (getProductByid.getUintsInOrder() < getProductByid.getUnitsInStock()) {
throw new IllegalArgumentException("OUT OF STOCK" + getProductByid.getUintsInOrder());
}
getProductByid.setUintsInOrder(getProductByid.getUintsInOrder() - count);
}
**@controller**



@Controller
public class OrderController {

@Autowired
OrderService orderService;

@RequestMapping("/order/A1234/2")
public String processOrder() {
System.out.println("process order");
orderService.processOrder("A1232", 2);
return "redirect:/product";
}
}

StackTrack

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/web_Store] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: no product found A1233] with root cause java.lang.IllegalArgumentException: no product found A1233 at com.webstore.repoImpl.ProductRepositiryImpl.getProductsById(ProductRepositiryImpl.java:58) at com.webstore.serviceImpl.OrderServiceImpl.processOrder(OrderServiceImpl.java:20) at com.webstore.controller.OrderController.processOrder(OrderController.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source)

在类 ProductRepositiryImpl 中,每当我尝试执行方法 getProductsById(String ProductId) 并且在调试时,我都会得到 productById = Product; 持有有效值,那么它应该退出循环为什么它仍然收到 IllegalArgumentException任何帮助将不胜感激

最诚挚的问候

最佳答案

public ProductRepositiryImpl() {
listOfAllProducts = new LinkedList<>();

Product iphone = new Product("A1232", "iPhone", new BigDecimal(500));
iphone.setDescription("iPhone 6s with 5.2' FHD Screen");
iphone.setManfactuer("Apple");
iphone.setCategory("Smart Phone");
iphone.setUnitsInStock(699);

listOfAllProducts.add(iphone);
}

您正在添加ID A1232的产品

@RequestMapping("/order/A1234/2")
public String processOrder() {
System.out.println("process order");
orderService.processOrder("A1233", 2);
return "redirect:/product";
}

您正在尝试获取ID A1233的产品

编辑1:

@Florian在上面的评论中说道。将 productById == null 的检查移到 for 循环之外

public Product getProductsById(String productId) {
Product productById = null;
for (Product product : listOfAllProducts) {
if (product != null && product.getProductId() != null && product.getProductId().equals(productId)) {
productById = product;
break;
}
}
if (productById == null)
throw new IllegalArgumentException("no product found " + productId);

return productById;

}

关于java.lang.IllegalArgumentException : no product found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38994267/

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