• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Applet giving NullPointerException?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does this applet throws NullPointerException? infact whenever i use for loop in init() the applet doesn't run in appletviewer. I am using Windows XP SP2. Here's the code
i am trying to create a space. Here's the code



I think i have allocated memory for every object that i am going to use then why this NullPointerException. I am breaking my head since 1 hour in vain (I've allocated memory for 1000 objects but i am using only 1st object, but i guess that shouldn't be the problem)
Please tell me the reason for this weird behaviour of applet
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You forgot to create a new instance of each object in the array that you use:
 
Nitesh Panchal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why again allocate the memory for individual objects?
I mean i allocated memory for 1000 objects altogether. Didn't i?



please tell me what does the above statement do?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chotta chatri wrote:But why again allocate the memory for individual objects?
I mean i allocated memory for 1000 objects altogether. Didn't i?


No



please tell me what does the above statement do?


It initializes an array object but does not initialize the array items. You have to do that yourself as I demonstrated.
 
Nitesh Panchal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so you have to do that this way only for every class?i mean say for String class i want to allocate memory for 10 objects

do i have to do it this way?


or this way?



>
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chotta chatri wrote:so you have to do that this way only for every class?i mean say for String class i want to allocate memory for 10 objects

do i have to do it this way?


String is an exception as you construct a String object (if necessary) by simply assigning a String literal to a variable. So otherwise, yes, you have to do this for any array of objects. Now this is not necessary for an array of primitives since they don't have to be constructed.
 
Nitesh Panchal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right! thanks you're the man
Now concept's getting clearer in my mind. Actually i am pretty new to Java and i knew how to allocate memory for primitive type array but memory for user defined class i didn't know that you have to do it this way!
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chotta chatri wrote:Right! thanks you're the man
Now concept's getting clearer in my mind. Actually i am pretty new to Java and i knew how to allocate memory for primitive type array but memory for user defined class i didn't know that you have to do it this way!


Good. Glad you've got it straight now. Just curious though, what are you trying to draw here? an orbit of some sort? Your image is off the screen, you know. Also, why choose to use AWT and not the more recent, flexible, and powerful Swing?
 
Nitesh Panchal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah orbit sort of things in which "circles" pop up from the space and keep revolving i am trying to figure out why is it going out of screen.
Yeah Swing is powerful but in our college course it's AWT no Swing ,in our course so i can't retialiate and ask them to keep Swing even though this is the most recent
 
No more fooling around. Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic