Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NodeList interface and NullPointerException

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
US
Hi,

I've noticed some strange behavior when using a NodeList and was wondering if others might have noticed this as well, or can point out something that I'm doing wrong...

According to the java.sun.com specification,
NodeList.item( int index )
returns either the node at the specified position (starting from 0), or null if index isn't valid. Well, in my application I receive a NodeList from
org.apache.xpath.XPathAPI.selectNodeList(Node, String) but
a NullPointerException is apparently being thrown when I
query the NodeList with an index value that is out of bounds (from above). Hmmm..., the XPathAPI specification doesn't mention anything about any exception being thrown by this method, just that a value of null should be returned for an out of bounds index value. Why would a NullPointerException result?? A value of null and a NullPointerException are very different things... I'm not feeding this result into anything else that does throw an NPE exeption either, so there isn't any confusion there.

Thanks!!!
dora
p.s., people have often suggested that I take a look at the source code for certain classes, such as HashMap or (I'd assume) XPathAPI. I'm probably being dense here, but where can I get that? :)

 
>> when I query the NodeList with an index value that is
>> out of bounds

can you post that part of the code?

-pete
 

sure...
The below replicates what I mentioned..

Code:
NodeList nlList = XPathAPI.selectNodeList( doc, sXPath );

int iListCount = nlList.getLength();

System.out.println("count is: " + iListCount );

try
{
	Node n = nlList.item( iListCount - 1 );

	System.out.println( "made it this far..." );

	n = nlList.item( iListCount + 1 );

	System.out.println( "do i get here?" );
}
catch ( NullPointerException npe )
{
	System.out.println( "exception encountered: " + npe.getMessage() );
};

yielding:
Code:
count is: 3
made it this far...
exception encountered: null

Thanks!!
dora


 
When i execute the following code...

Code:
		NodeList nl = root.selectNodes("//test");
		System.out.println("COUNT.test: " + nl.getLength());
		Node n = nl.item(10);
		System.out.println("Node is null: " + (null == n));

I get the following output
C:\Projects\Forte\projects\bak\RndDom>java RndDom
COUNT.test: 2
Node is null: true



-pete
 
What type of object is "root" in your example? I'm used to using the XPathAPI for XPath queries of a DOM, and actual node objects don't have selectNodes() methods, so it must be something else?

Thanks,
dora
 
Great question!

It’s just a function in a simple class I have that decorates the org.w3c.dom.Element interface. In this code the variable _e a the reference to the DOM Element
Code:
	public NodeList selectNodes(String xpQuery)
		throws SAXException
	{
		
		XMLParserLiaison oParser = new XMLParserLiaisonDefault();
		XPathProcessor xpProcessor = new XPathProcessorImpl(oParser);
		XPath xp = new XPath();
		PrefixResolver pr = new PrefixResolverDefault( _e);
		xpProcessor.initXPath( xp, xpQuery, pr);
		XObject list = xp.execute( oParser, _e, pr);
		return list.nodeset();
	}

-pete
 
Ahhhh, slightly different processing than XPathAPI. Would you recommend your approach over something like XPathAPI? I only use that because it's what I'm aware of, but as always I'd like to do whats accepted as "best".

I'm sort of inclined to switch away from XPathAPI as it is, because apparently it doesn't behave exactly as advertised! As far as I can tell, it is pretty unambiguously throwing unadvertised exceptions, which can make for very difficult to debug programs! So, all of the necessary objects (XPath, etc.) that you use above can be found in org.w3c.dom?

Thanks,
dora
 
I don’t think so. Actually the code I use was developed prior to the existence of XPathAPI. My plan is to migrate to using XPathAPI rather than my old class. Here are the imports I am using with the code I posted:

import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.apache.xalan.xpath.*;
import org.apache.xalan.xpath.xml.*;
import org.apache.xml.serialize.*;

As you can see I am using the Xalan xpath implementation so there should not be a problem with what you are using. Besides it is the NodeList object you seem to be having problems with not the XPath implementation.

In your previous post you stated[blue]
sure...
The below replicates what I mentioned..[/blue]

perhaps there is an actual error in your real code rather than the facsimile that you posted.


-pete
 
Well, the facsimile replicates the condition I describe...
so whatever problem i have in my "real" program is also present in the fax. The difference in our results must be due to the fact that I'm using XPathAPI.

the apache website describes the item() method on the NodeList interface as follows:
Code:
item

public Node item(int index)Returns the indexth item in the collection. If index is greater than or equal to the number of nodes in the list, this returns null.

Parameters:
    index - Index into the collection.
Returns:
    The node at the indexth position in the NodeList, or   null if that is not a valid index.

This is precisely the same definition I find at the Sun website...

So - perhaps XPathAPI is returning an implementation of NodeList that deviates from the standards listed above in that it throws a NullPointerException for invalid index values. This is legal - implementations of interfaces don't have to agree on what is thrown (which always seemed weird to me - why is that allowed to compile??).... AND, to make matters worse, NullPointerException is a subclass of RuntimeException.. this means that it can be thrown without being listed in a "throws" clause. My guess (just a guess, don't have access to the XPathAPI source) is that whatever they return as an implementation of NodeList does one of these two things (it either lists NullPointerException in "throws" and gets away with it because implementing subclasses don't need to match the interface on the throws clause, or doesn't list it in "throws" by takes advantage of the fact that NullPointerException is a RuntimeException).

In either case, I'm going to specifically check for NullPointerException (in spite of the documentation), and that should solve my issues...

Thanks -
dora
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top