[jdom-interest] why can't I sort this list?

Paul Phillips paul at partitura.com
Tue Jul 23 20:33:02 PDT 2002


I am sure I am doing something really stupid here, but...

I have a list that I have obtained reading in an xml file using jdom. I can 
read from and write to this file.  I just can't sort it - I get a null 
pointer exception that I can't figure out...

The xml file is simple.
The root element is <users>
There are several <user> children.
Each <user> child has one <name> element.

Here is the code excerpt...

	if (userxmlfile.exists()) {
            try {
		SAXBuilder builder = new SAXBuilder();
		Document myDocument = builder.build(userxmlfile);
		Element topElement = myDocument.getRootElement();

  // now get the list
   List theListOfUsers = topElement.getChildren();
   // now alphabetize
   // this statement produces the null pointer exception.
   Collections.sort(theListOfUsers, new SortUsers());

....

at the bottom of the same file containing the above code, I have this class 
defined...

 class SortUsers implements Comparator  {
        public int compare(Object o1, Object o2) {
        Element d1 = (Element)o1;
        Element d2 = (Element)o2;
        String s1 = d1.getChild("name").getText();
        String s2 = d2.getChild("name").getText();
        return (s1.compareTo(s2));
        }
    }

Any help would be appreciated...
Thanks
Paul Phillips



More information about the jdom-interest mailing list