• 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

NullPointerException - but why?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry friends, this might turn out to be a really stupid doubt, but
i'm confused..

I dont get why this piece of code throws a NullPointerException at line 5



Thanks
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to initialize an array before using it.
For example String[] arr = new String[1];
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Radha:

Add "static" to the array declaration to access it from the main method, and instantiate the array.




Originally posted by Radha Kamesh:
Sorry friends, this might turn out to be a really stupid doubt, but
i'm confused..

I dont get why this piece of code throws a NullPointerException at line 5



Thanks

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are getting a NullPointerException because you are trying to access an Array which has not been initialized, it is currently NULL. Before you access the Array element you need to make sure it is initialized e.g.

String[] arr = new String[1]

this will then allocate an Array object in the Heap memory with 1 space where you can then assign youre String data.
arr[0] = s;


Thanks
Siphiwe M

SCJP 1.5 (aspirant)
 
reply
    Bookmark Topic Watch Topic
  • New Topic