Code Generates Null Pointer Exception

Question:
If I have:

class point { double x,y; }  &  import point;  class test {        public static void main(String args[])        {         point[] P = new point[10];        P[0].x = 5;        P[0].y = 34874;        } } 
it generates a null pointer exception. Why?

Answer:
Try:

P[0] = new Point();   P[0].x = 5;   etc. 
Your array declaration allocates an array large enough to holdonly 10 pointers to points. Space to hold each point must be allocatedseparately.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist