[jdom-interest] Document getRootElement() problem

Elliotte Rusty Harold elharo at metalab.unc.edu
Wed Jun 14 14:50:58 PDT 2000


In Document.java I noticed the following:

    /**
     * <p>
     * This will return the root <code>Element</code>
     *   for this <code>Document</code>, or null if there is none.
     * </p>
     *
     * @return <code>Element</code> - the document's root element
     * @throws <code>NoSuchElementException</code> - when no root
     *                                      element exists.
     */
    public Element getRootElement() throws NoSuchElementException {
        if (rootElement == null)  {
            throw new NoSuchElementException("No root element exists.");
        }
        return rootElement;
    }

Contrary to the JavaDoc comment, this method never returns null. It
throws a NoSuchElementException instead. One or the other needs to be
changed.

Personally, my preference is, from most preferred to least preferred:

1. Prevent creation of Document objects with null root elements.
2. Allow getRootElement() to return null if the root element is null.
3. Change the JavaDoc to correctly describe the current behavior. 

-- 
+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+ 
|               Java I/O (O'Reilly & Associates, 1999)               |
|            http://metalab.unc.edu/javafaq/books/javaio/            |
|   http://www.amazon.com/exec/obidos/ISBN=1565924851/cafeaulaitA/   |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ | 
|  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |
+----------------------------------+---------------------------------+



More information about the jdom-interest mailing list