Given the Point class and array what would be the result of
Given the Point class and array, what would be the result of accessing the first Point in the array?
| ArrayIndexOutOfBoundsException |
Solution
Ans: NullPointerException
So, line : Point[] points = new Point[10]; => all 10 elements( 10 Point object) initilizes with \'null\'
Hence when you do: points[0].x or points[0].y => it throws \"NullPointerException\" because points[0] is poinitng to null
To access you need to create Object first, like this:
points[0] = new Point();
Now you can access: System.out.println(points[0].x + \" \" + points[0].y); // output: 0.0 0.0
