I am having trouble initialize a method in the code below.
The code below shows a class called CharacterHandler which reads an XML
file. The characters in this XML file have to be encoded in a special
way In order to encode the characters, I am trying to use another class
called Encode.
I wanted to create on method of the class Encode. That way, each time
the main (CharacterHandler) class finds characters, it call on the
method in Encode to return a string of encoded characters.
However, my constructor does not seem to be working. The constructor
does initiatilize a String variable called location. However, it does
not seem to be able to initilialize the method so that I can use it
later.
The code will not compile because it finds "encodeHandler" in the method
characters. The actual error message is this:
CharacterHandler.java:48: cannot resolve symbol
symbol : method encodeHandler (char[],int)
location: class CharacterHandler
String the_characters = this.encodeHandler(ch, length);
^
1 error
public class CharacterHandler extends DefaultHandler
{
Encode encodeHandler;
String location;
CharacterHandler(String theLocation){
location = theLocation;
//note this is just a test for now. Really I need to create
// the method and then pass "1252" to it later.
encodeHandler = new Encode(location, "1252");
}
public void startElement (String namespaceURI, String localName,
String qName, Attributes atts)
throws SAXException
{
if (localName.equals("doc")){
String codePage = atts.getValue("encoding-page");
if (codePage != null){
//this doesn't give me an error.
encodeHandler = new Encode(location, codePage);
}
}
}
public void characters (char ch[], int start, int length)
throws SAXException
{
//ERROR!
String the_characters = encodeHandler(ch, length);
System.out.print(the_characters);
}
}