Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

NullPointerException when i would like to add data into an array

 
Ranch Hand
Posts: 34
Eclipse IDE Firefox Browser Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem at my code. When i would like to add the textChild.getNodeValue() to the Datay array, i have got the java.lang.NullPointerException.
I have to use JDK 1.4.




I would like to add the results to the array. Does someone have any idea how to change this code?
Thank you in advance for any help you can provide.
 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Bloom wrote:I have a problem at my code. When i would like to add the textChild.getNodeValue() to the Datay array, i have got the java.lang.NullPointerException.
I have to use JDK 1.4.




I would like to add the results to the array. Does someone have any idea how to change this code?
Thank you in advance for any help you can provide.



What does the stacktrace says about the line number? Does "System.out.println(textChild.getNodeValue());" print anything?
And doesn't this throws compilation error Datay has not been initialized?
 
Ranch Hand
Posts: 2108
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should instantiate Datay first before assigning data on it.

e.g.

Datay = new String[5];
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Datay[i] = textChild.getNodeValue(); // java.lang.NullPointerException

2 things: 1) where did you initialize Datay array? 2) what is the value of getNodeValue?

as a side note: declaring variable name starting with capital letter is poor practice.
 
Marshal
Posts: 74390
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yo mean apart from why have you got so many static members?
You have declared the array as a field and never instantiated anything. You will find examples of instantiating arrays in the Java Tutorials.
 
Edward Bloom
Ranch Hand
Posts: 34
Eclipse IDE Firefox Browser Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Palak! I changed my code like this, but i still have the NullPointerException problem:



My output:
Root element: books
nList length: 1260
0 DRIFT, by Rachel Maddow
java.lang.NullPointerException

Seetharaman!
"where did you initialize Datay array?" : As i have known i do not have to initialize the static array.
"what is the value of getNodeValue?" : String org.w3c.dom.Node.getNodeValue() throws DOMException

I have noted your note! (: I am a beginner in the Java.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Bloom wrote:
"where did you initialize Datay array?" : As i have known i do not have to initialize the static array.



That is incorrect. Static or not, every variable must be initialized before it will point to an object, and every array element is null, 0, or false unless you give it a value.
 
Palak Mathur
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Bloom wrote:Palak! I changed my code like this, but i still have the NullPointerException problem:



My output:
Root element: books
nList length: 1260
0 DRIFT, by Rachel Maddow
java.lang.NullPointerException

Seetharaman!
"where did you initialize Datay array?" : As i have known i do not have to initialize the static array.
"what is the value of getNodeValue?" : String org.w3c.dom.Node.getNodeValue() throws DOMException

I have noted your note! (: I am a beginner in the Java.



Try using: static String[] arr = new String[5];

 
Palak Mathur
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Palak Mathur wrote:

Edward Bloom wrote:Palak! I changed my code like this, but i still have the NullPointerException problem:



My output:
Root element: books
nList length: 1260
0 DRIFT, by Rachel Maddow
java.lang.NullPointerException

Seetharaman!
"where did you initialize Datay array?" : As i have known i do not have to initialize the static array.
"what is the value of getNodeValue?" : String org.w3c.dom.Node.getNodeValue() throws DOMException

I have noted your note! (: I am a beginner in the Java.



Try using: static String[] arr = new String[5];



You will have to create a String array if you want to use the array.
 
Edward Bloom
Ranch Hand
Posts: 34
Eclipse IDE Firefox Browser Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesus, Seetharaman , Jeff! You are right! The problem is solved with the initialize.



Thank you so much!
Have a nice day!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edward Bloom wrote:Thank you so much!Have a nice day!


You are welcome , you too have a nice day .
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic