NoSuchMethodError

This code compiles, but I'm getting a NoSuchMethod error when I try to run it. This error seems to make no sense, because the countBooks method was just created in the BookCounter class. What am I missing here? Thanks in advance for your help:

class BookCounter extends HandlerBase {

private int count = 0;

String url;

public void countBooks(String url) throws Exception {

this.url = url;

Parser p = new com.jclark.xml.sax.Driver();

p.setDocumentHandler(this);

p.parse(url);

}

public void startElement(String name, AttributeList atts) throws SAXException {

if (name.equals("book")) {

count++;

}

}

public void endDocument() throws SAXException {

System.out.println("There are " + count + " books");

}

}

public class TestBookCounter {

public static void main(String args[]) throws Exception {

String url = "http://www.mysite.com/Test/books.xml";

BookCounter bc = new BookCounter();

bc.countBooks(url);

}

}

[1075 byte] By [vincent11867] at [2007-9-26 3:26:26]
# 1

You don't have your complete code posted, so I took what you did post, altered it where needed, and then compiled. This could will compile and run fine.

public class TestBookCounter {

public static void main(String args[]) throws Exception {

String url = "http://www.mysite.com/Test/books.xml";

BookCounter bc = new BookCounter();

bc.countBooks(url);

}

}

class BookCounter {

private int count = 0;

String url;

public void countBooks(String url) throws Exception {

this.url = url;

System.out.println(url);

}

}

Start adding back the missing elements until you isolate the offending item.

Good luck.

GLJdotcom at 2007-6-29 11:48:11 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2

Where exactly is the NoSuchMethodException ocurring? I doubt it's talking about countBooks. My guess is that p.setDocumentHandler or p.parse is using reflection to invoke a particular method on your BookCounter, and it's not finding that method, or not finding it with the right signature. Look closely at the stack trace, and post it if you still can't find it.

jverd at 2007-6-29 11:48:11 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
looking up Parser in the Nutshell book for JFC, Parser has no method setDocumentHandler()!That is probably it.
Sinprejic at 2007-6-29 11:48:11 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

And if com.jclark.xml.sax.Driver extends/implements Parser, and com.jclark.xml.sax.Driver does have a setDocumentHandler method, then what you need to do is either declare p as com.jclark.xml.sax.Driver in the first place, or cast it when you use it.

Declaring a reference as the abstract class or interface is useful when you intend to use it without regard to which particular implementation you have. Then you can change which impelmentation you're using without impacting code that, in your case, for example, knows only that p is a Parser. Since you're explicitly using functionality that is explicit to com.jclark.xml.sax.Driver, you may as well declare it as that right off the bat.

jverd at 2007-6-29 11:48:11 > top of Java-index,Archived Forums,New To Java Technology Archive...