jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes a problem at run time with a call to a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a problem at run time with a call to a method" Watch "a problem at run time with a call to a method" New topic

a problem at run time with a call to a method

Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
I am having a problem calling a method revComp() within my class DNASequence. DNASequence has concrete methods and extends my class Sequence. The programme compiles and I think that the logic is correct but there is an error reported when I try to call the method. I was hoping that someone would be able give me a hint as to where I have gone wrong.

The method is designed to take the content string from an object (DNASequence which is composed of 2 strings one for description and one for content). The content string is reversed and then the complementary sequence is generated. This is then copied into a new DNASequence object along with the original description.

In main I create a new DNASequence object (tbx20) and then call the revComp method with this object.

DNASequence revComplement = tbx20.revComp();

But I get the following error when the programme is run, which I think implies the programme does not recognise the object tbx20, but I do not know why this would be.

---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.5.0\bin\javac.exe" DNASequence.java
DNASequence.java:87: cannot find symbol
symbol : variable tbx20
location: class DNASequence
DNASequence revComplement = tbx20.revComp();
1 error
> Terminated with exit code 1.

I have pasted the code for the DNASequence class below. Hope someone can help.

Best wishes
Dianne Gerrelli

public class DNASequence extends Sequence{

/**Class methods*/
public DNASequence (String description, String content)throws InvalidSequenceException{
super(description, content);

/**This method takes the content string of a DNASequence and reverses it.
It then produces the complementary sequence from the reversed strand.
It then returns this as a new DNASequence object.
This compiles but produces an error when the method is called*/

public DNASequence revComp ()throws InvalidSequenceException {

String content = getContent();
String description = getDescription();
String complement = null;

int length;
length =content.length();

StringBuffer reverse = new StringBuffer(length);
for (int i = (length - 1); i >= 0; i--)
String reversedSequence = new String (reverse);

for (int x = 0; x < reversedSequence.length(); x++)
char character = reversedSequence.charAt(x);

if (character == 'a') complement += 't';
else if (character == 'c') complement += 'g';
else if (character == 'g') complement += 'c';
else if (character == 't') complement += 'a';
{throw new InvalidSequenceException (x, content);}

DNASequence reverseComplement = new DNASequence (description, complement);
return reverseComplement;

public String getDescription(){
return description;

public String getContent (){
return content;

public String validLetters() {
String letters = "actgt";
return letters;

public String toString(){
return "the DNA description is " + this.getDescription() + " the DNA content is "

public static void main (String[] args){

try {DNASequence tbx20 = new DNASequence(">DNA1", "actgyttt");}

catch (InvalidSequenceException e) {

System.out.println( "invalid character at " + e.getMessage() );}

finally {

System.out.println( "That's all, folks" );}

/** This call to the method revComp causes an error with the
programme appearing not to recognise the variable tbx20*/

DNASequence revComplement = tbx20.revComp();

System.out.println (revComplement);

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Note: let's get our terminology straight, first. This is a compiler error, not a runtime error. One way to tell, if you aren't sure whether you've run javac or java, is the "C:\Program Files\Java\jdk1.5.0\bin\javac.exe" in the message.

Now, as to your problem: a set of curly braces define a "scope". In general, local variables defined inside a scope are visible in that scope and in scopes nested inside of that one; they're not visible in "larger" scopes outside the one where the variable is declared. You've defined "tbx20 " inside of a one-line "try" block, so that variable's scope is limited to that one line. What you need to do is either make the try block bigger by moving all the following code into it (which is the generally preferred way to do things, when it applies) or, alternatively, move the declaration of the variable outside the try block and then initialize it inside the try, like

// Other code down here can now see tbx20 because it's declared outside the
// try block.

[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Note that if you are running the javac command, then the errors are compile-time errors. You can only get run-time errors if you use the java command to actually run your program.


Java API Documentation
The Java Tutorial
Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
Thanks very much for pointing out my errors and inaccuracies.

Best wishes
Dianne Gerrelli
I agree. Here's the link: http://aspose.com/file-tools
subject: a problem at run time with a call to a method
It's not a secret anymore!