Hi,
I have 2 classes:
1) productType that have name and price of the productType and an empty consturcor.
2) superMarket that have name of supermarket and arraylist of productTypes () and an empty constructor.private ArrayList<ProductType> products;
In each class I have function that get input from console and should store it into each class variables.
in productType i have function:
public void getFromUser() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter product name:"); name = br.readLine(); System.out.println("Enter price:"); price = Integer.parseInt(br.readLine()); }
in supermarket i have function:
public void getFromUser() throws IOException { int n=0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); ProductType p = new ProductType(); System.out.println("Enter supermarket details: "); System.out.println("Enter name: "); this.name = br.readLine(); System.out.println("How many products do you have: "); n = Integer.parseInt(br.readLine()); for(int i=0;i<n;i++) { products.add(i,p); products.get(i).getFromUser(); } }
the main is something like:
public static void main(String[] args) throws IOException{ SuperMarket s1 = new SuperMarket(); SuperMarket s2 = new SuperMarket(); s1.getFromUser(); }
The problem is when i get to line "products.add(i,p)" I get java.lang.NullPointerException
In the debug mode I can see that when I get to this line the "products" is null.
why do you think this happening, when I do "new SuperMarker()" in the main it should run the empty constructor and create new arraylist...
?