• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

adding more attributes+DOM

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an xml file
<COURSE>
<exam date="13-feb-2006" type="v">
.....
</exam>
<STUDENT id="studid">

</STUDENT>
...�..
</COURSE>

I would like to add attribute firstname to STUDENT element(using DOM) so that it shows information like this
<COURSE>
<exam date="13-feb-2006" type="v">
.....
</exam>
<STUDENT id="id" firstname="maggie" >

</STUDENT>
...�..
</COURSE>

I tried running this code

But I get this error, when I run the program

java.lang.NullPointerException
at DOMAdd.main(DOMAdd.java:49)

Is there something wrong with the coding?

-M
[ February 21, 2006: Message edited by: Maggie Taylor ]
 
Sheriff
Posts: 27456
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you get a NullPointerException then by definition there's an error in the coding.

In your case, after this line of codeis executed, the "element" variable is null. That's because you made the common mistake of assuming that the getElementById() method looks for elements that have an attribute whose name is "id". It doesn't. It looks for elements that have an attribute that is defined, by a DTD or a schema, to be an "ID" attribute.

But you don't have anything that describes the document content like that. So you will have to write code that looks at all the <STUDENT> elements and adds an attribute to the right ones. The getElementsByTagName() method will help here. And by the way, the line of codeis unnecessary (and possibly harmful). The element is already part of the document and looking at it doesn't change that.
 
Maggie Taylor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:


In your case, after this line of code Element element = doc.getElementById("studid"); is executed, the "element" variable is null. That's because you made the common mistake of assuming that the getElementById() method looks for elements that have an attribute whose name is "id". It doesn't. It looks for elements that have an attribute that is defined, by a DTD or a schema, to be an "ID" attribute.



Thanx Paul, I learnt something new. I added the attribute "id" to the DTD, and then used the code. And it works fine.

-M
 
The world's cheapest jedi mind trick: "Aw c'mon, why not 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