Code Ganja Code Ganja - 3 months ago 18
Java Question

java.lang.IllegalArgumentException: no product found

Hi while learning spring I got stuck in following code not sure what is wrong with this code

@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)


In class
ProductRepositiryImpl
whenever I am trying to execute method
getProductsById(String productId)
and while debugging I get
productById = product;
holding a valid value then it is supposed to exit loop why it is still getting
IllegalArgumentException

any help will be appericiated

Best Regards

Answer
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);
    }

You are adding the product with ID A1232

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

You are trying to get the Product with ID A1233

EDIT 1:

As @Florian said on comment above. Move check for productById == null outside of for loop

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;

}
Comments