• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException when i would like to add data into an array  RSS feed

 
Edward Bloom
Ranch Hand
Posts: 34
Eclipse IDE Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • 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.
 
Palak Mathur
Ranch Hand
Posts: 343
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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?
 
Jesus Angeles
Ranch Hand
Posts: 2070
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should instantiate Datay first before assigning data on it.

e.g.

Datay = new String[5];
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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 Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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 Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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 .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!