Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Can't initialize method

  Asked By: Elliott    Date: Nov 28    Category: Java    Views: 597
  

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);
}




}

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Guadalupe Rogers     Answered On: Nov 28

Where's the method  called encodeHandler (char[],int) ?, did you declare
it in Encode class  ?. In that case, dont you have to qualify it with
the instance name ?, ie Encode.encodeHandler (char[],int), assuming you
declared it as a static method.

 
Answer #2    Answered By: Gustavo Taylor     Answered On: Nov 28

It's amazing how writing clarifies your thinking. No sooner had I sent
off this email than I immediately realized my mistake, whichh I had been
trying to understand for 2 hours.

Encode is an object--not a method!

As soon as I changed

String the_characters = encode(ch);

to


String the_characters = encode.getNewEncoding(ch);

Everything worked as it should.

I tried to post my correction, but my email didn't show up right away. I
checked again and again and after an hour I had to get to bed.

 
Didn't find what you were looking for? Find more on Can't initialize method Or get search suggestion and latest updates.




Tagged: