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.

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.

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.

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;
          }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.