• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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)
 
We can walk to school together. And we can both read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic