In my code there is a 'Null Pointer Exception' but I can't seem to understand why. I've added the objects into the ArrayList in the constructor. When I try run the method, I get a Null Pointer Exception. Any help would be greatly appreciated.
private ArrayList products;
//PRODUCTS CLASS CONSTRUCTOR
Products(){
ArrayList<ProductsClass> products = new ArrayList<ProductsClass>(); //Declaration and instantiation of ArrayList
products.add(new ProductsClass("Bread", 0.65));
products.add(new ProductsClass("Milk", 2.00));
products.add(new ProductsClass("Pasta", 3.35));
products.add(new ProductsClass("Milk chocolate bar", 0.75));
products.add(new ProductsClass("Dark Rum", 25.00));
ProductsClass p = products.get(3);
System.out.print(p.getItemName() + ""); //Testing to see if works#
System.out.print("");
System.out.print(" " + p.getItemPrice());
}
public void findMatchingItem() {
for (ProductsClass product : products) {
System.out.print(product);
}
}