Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Mar 21, 1997

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 hold only 10 pointers to points. Space to hold each point must be allocated separately.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date