guys, why am I getting NullPointerException when initializing the array? I ran debug mode and productName is null
public class Main { public static void main(String[] args) { Product[] myProducts = new Product[3]; Supplier ikea = new Supplier(); ikea.supplierName = "ikea"; myProducts[0].productName = "chair"; myProducts[0].supplierName = ikea; myProducts[1].productName = "table"; myProducts[1].supplierName = ikea; Supplier javaBlackBelt = new Supplier(); javaBlackBelt.supplierName = "javaBlackBelt"; myProducts[2].productName = "OO Exam"; myProducts[2].supplierName = javaBlackBelt; for(Product p: myProducts) { System.out.println(p.productName+" is from "+p.supplierName); } } }
public class Product { String productName; Supplier supplierName; }
public class Supplier { String supplierName; }