-3
class Pointgrejen{
    public static void main (String [] args){
        int[] a = new int[5];
        a[3] = 7;

        Point[] b = new Point[5];
        b[3].x = 7; 

        System.out.print(a[3]);
        System.out.print(b[3]);
    }
}

Why isn't the program executing? Point is a java class in java, but is the error in this code that I havent defined what type of data the array should hold in the point array? Or whats the bigger issue with this code?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Mr Miyagi
  • 31
  • 6

2 Answers2

1

You created the array, so the array values will be null by default.

Means b[3] will be null, so you will get NullPointerException in below line of code:

Point[] b = new Point[5];
b[3].x = 7; 

So to fix it create objects and assign them to array:

b[3] = new Point();
b[3].x = 7
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
0

You created the points array, but is filled with nulls so to assign b[3].x = 7; you must first do b[3] = new Point();