954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using a method of an object - cannot find variable

Hi, I'm fairly new to java and am trying to write a form of banking system for a college assignment. At the moment I am trying to access the account number of the object userCurrentAccount with it's method getAccountNumber from the account class.

This is where the object is created and gets the account number and balance set:

CurrentAccount userCurrentAccount = new CurrentAccount();
userCurrentAccount.setCABalance(fileCurrentAccountBalance);
userCurrentAccount.setAccountNumber(fileCurrentAccountNum);


In the separate jFrame form I currently have this code but I get an error saying 'cannot find variable' regarding userCurrentAccount:

String accnum;
accnum = userCurrentAccount.getAccountNumber();


Any help greatly appreciated.

keppy
Newbie Poster
1 post since May 2009
Reputation Points: 10
Solved Threads: 0
 

It's almost certainly a scoping issue or Java doesn't know which class to look in. Where is this line located (what class is it in and is it a global variable?)?

CurrentAccount userCurrentAccount = new CurrentAccount();


Is it in the same class as this code?

accnum = userCurrentAccount.getAccountNumber();


You mention that the code above is in "a separate jFrame". If the two code snippets are not in the same class, then the code above isn't going to be able to find userCurrentAccount . If the two code snippets are in the same class, but userCurrentAccount is not a global variable and the two code snippets are not in the same function, you're also likely to get that error. I can't speculate more though. I'd have to see more of the code, but my strong guess is that the variable can't be found without specifying the class or it is a local variable that has gone out of scope. You may need to post more code.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Continuing from what Vernon said - to display an object (account) in a GUI you need a reference to that specific object, which typically comes from (a) a prior bit of GUI where you select an account or (b) the object itself if it decides it wants to be displayed. Either way you would typically pass the object reference (userCurrentAccount) into the JFrame's constructor, thus giving the JFame access to object and its methods.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Find the answer from the post #2 and #3. Both, JC and vernon suggest you about scope of variable. You have scope issue with object variable. You have to pass reference of an object through a method.

class One {
    ...        
}
class Two {
     // Object  of class one
   One a=new One();
   Three b;
    ....
    ....
   // Now you want this object to be available into class Three
   void something() {
         b=new Three(a); // Passing a reference of an object.
    }
}
class Three {
         One v;
         public Three(One v) {
                this.v=v;
          }
}
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You