I can't solve NullPointerException problem (82 line). It's simple code with interface. In final result it have to print 10 random shapes with its parameters and total area of shapes. Final result should look like in picture.
Asked
Active
Viewed 33 times
0
-
You need to create a object of `Shape` like this `shapes[i]=new Shape()` before you call `print()` method on it. – Pradeep Simha Jun 03 '21 at 05:39
-
@PradeepSimha Well then problem is: Shape is abstract; cannot be instantiated shapes[i] = new Shape(); `interface Shape { Random rand = new Random(); double area(); void print(); } // first class, Circle class Circle implements Shape { double PI = 3.14D; int r; public Circle() { this.r = rand.nextInt(9) + 1; } public double area() { return this.PI * (double)this.r * (double)this.r; } public void print() { System.out.println("Circle: " + this.r); } }` – Audranas Jun 03 '21 at 07:16
-
Well you should have posted that in question itself instead of image! Then you cannot instantiate it, you need to have a class implements that interface and create a instance of that class instead of `Shape`. – Pradeep Simha Jun 03 '21 at 07:24
-
@PradeepSimha I have 3 classes implements interface Shape, they have fields to save shapes measurements, constructors with parameters and methods area and print. Where I should create a instance of that classes? – Audranas Jun 03 '21 at 08:12