Page 3 of 3 FirstFirst 123
Results 41 to 55 of 55
Like Tree1Likes

Thread: Can't call variable from other class

  1. #41
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can't call variable from other class

    What errors do you get when you compile the code you posted?
    The assignment statement at line 18 is reversed.
    If you don't understand my response, don't ignore it, ask a question.

  2. #42
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    The compiler don't detect any erros in the file. The erros only appear when we add more args to the constructors.

    maybe we are not understanding well what u mean by arguments.

  3. #43
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can't call variable from other class

    If there are no errors, what is your question?
    Post the code with the errors and the error messages you get.

    Parameter and argument are the words used to describe the variables coded inside of the ()s when a method or constructor is called:
    someMethod(<the arguments go here>);
    someMethod(a, b); // a & b are arguments to someMethod
    public void someMethod(int asd, int bsd) { // asd and bsd are the parameters to the method
    If you don't understand my response, don't ignore it, ask a question.

  4. #44
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    We don't have an error, everything is run good without errors but the things don't work how we want and we don't know why.

    Our problem is that we start that frame only by making it visible:

    Java Code:
     bContinue.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                    CombateFrame.setVisible(true);
                   
                }
            });
        }
    So we don't know where to write the arguments. Should we create here a constructor with the arguments?

  5. #45
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can't call variable from other class

    Is there a variable named: CombateFrame that has the value of a frame/refers to a window? Your code is using that variable to call the class's setVisible() method.

    we don't know where to write the arguments
    What arguments for what method or what constructor?
    If you have created an instance of a class the extends a window class and you want to make it visible, what you have coded should do that.

    things don't work how we want
    Please explain.


    EDIT: Having a variable name spelled the same as a class name is very confusing. CombateFrame is a class name. combateFrame is a variable name. Notice the case.
    Last edited by Norm; 05-03-2012 at 08:47 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #46
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Quote Originally Posted by Norm View Post
    Is there a variable named: CombateFrame that has the value of a frame/refers to a window? Your code is using that variable to call the class's setVisible() method.
    Java Code:
    CombateFrame CombateFrame;
    CombateFrame is the name of the class for where we want to send the variables. Should we do:
    Java Code:
    CombateFrame(args here) CombateFrame
    ?

    We have a class called Champs1 that uses a "frame" created by NetBeans. The button "bContinue" when selected activates another frame: CombateFrame. But it only makes it visible as u can see in the previous pasted code.

  7. #47
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can't call variable from other class

    we want to send the variables
    What method or constructor in the class do you want to pass the variables to?
    Use the constructor when creating an instance of the class.
    Use a method if an instance of the class already exists.
    If you don't understand my response, don't ignore it, ask a question.

  8. #48
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Ps. We will take care of the variable name mistake...

    We would like to pass the variables from the Champs1 class to the CombateFrame class constructor for testing purposes, and then to methods in the CombateFrame class.

    - Just to see how it works, we want to pass the Champ1 string variable value to the class CombateFrame so we can use it there with the value assigned in the Champs1 Class. Looking at code, could you please write what we should do to work?

    We don't want the work done, but I thing we are getting confused here and seeing what we should be doing might help...

    Java Code:
    public class Champs1 extends javax.swing.JFrame implements ActionListener {
    
        //Variavéis
        MainMenu champs1Frame;
        ChampsList ChampsList = new ChampsList();
        Combate Combate = new Combate();
        //Champs1 Champs1Obj = new Champs1(Champ1);
        CombateFrame CombateFrame;
        public String Champ1, Champ2;
        public double HP1, HP2, attack1, attack2, defense1, defense2;
        public int champ1Ref, champ2Ref;
        private int player = 1;
    
    
        public Champs1(MainMenu mainFrame) {
           
    
            mainFrame = champs1Frame;
    
            CombateFrame = new CombateFrame(this);
    
            initComponents(); //Desenhar frame
    
    
            bAntonio.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
    
    
                    ChampsList.antonio();
    
                    bAntonio.setEnabled(false);
    
                    if (player == 1) {
                        Champ1 = ChampsList.Champ;
                        attack1 = ChampsList.Attack;
                        defense1 = ChampsList.Defense;
                        HP1 = ChampsList.HP;
                        champ1Ref = ChampsList.champRef;
                        jLabel5.setText(Champ1);
                    }
    
                    if (player == 2) {
                        Champ2 = ChampsList.Champ;
                        attack2 = ChampsList.Attack;
                        defense2 = ChampsList.Defense;
                        HP2 = ChampsList.HP;
                        champ2Ref = ChampsList.champRef;
                        jLabel6.setText(Champ2);
                    }
    
                    player++;
    
    
                }
              
            });
    
            bContinue.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                    CombateFrame.setVisible(true);
                    //Combate.combate(Champ1, player);
                }
            });

    Java Code:
    public class CombateFrame extends javax.swing.JFrame {
     
        Champs1 CombateFrame;
         
        MainMenu mainRef;
        
        String champ1Name;
        
         
                 
        //Champs1 Champs1Obj = new Champs1(Champ1);
            
        /**
         * Creates new form CombateFrame
         */
        public CombateFrame(Champs1 CombateFrame_) {
             
            //champ1Name = Champs1Frame.Champ1;
             
            CombateFrame_ = CombateFrame;
     
            initComponents();
             
            lChamp1Name.setText(champ1Name);
     
        }

  9. #49
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can't call variable from other class

    want to pass the Champ1 string variable value to the class CombateFrame
    line 20 creates an instance of the CombateFrame class. Can you pass value there?

    Where is the variable named: value defined? I can not see it.

    What variable do you want to pass the CombateFrame object? Where and when is the value of that variable available?
    If you don't understand my response, don't ignore it, ask a question.

  10. #50
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Line 9 the variable is defined and line 36 a value to the variable is given. I want to pass the Champ1 variable.

    line 20 creates an instance of the CombateFrame class. Can you pass value there?
    I will try that.

  11. #51
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Ok, I changed line 20 to this:

    Java Code:
    CombateFrame = new CombateFrame(Champ1);
    and CombateFrame class to:

    Java Code:
    public class CombateFrame extends javax.swing.JFrame {
    
        Champs1 CombateFrame;
        
        MainMenu mainRef;
       
        String champ1Name;
       
        
                
        //Champs1 Champs1Obj = new Champs1(Champ1);
           
        /**
         * Creates new form CombateFrame
         */
        public CombateFrame(String Champ1) {
            
            //champ1Name = Champs1Frame.Champ1;
            champ1Name = Champ1;
            //CombateFrame_ = CombateFrame;
    
            initComponents();
            
            lChamp1Name.setText(Champ1);
    
        }
    No errors are detected but nothing appears on the textview defined here: lChamp1Name.setText(Champ1); so I suppose the Champ1 variable stills with no value at all.

  12. #52
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Sorry, that last problem was a mistake, I just figured out what was wrong. I guess is working now...

  13. #53
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can't call variable from other class

    You should change all your variable names to start with lowercase letters. The way you have defined variable names is confusing.
    If you don't understand my response, don't ignore it, ask a question.

  14. #54
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Quote Originally Posted by Norm View Post
    You should change all your variable names to start with lowercase letters. The way you have defined variable names is confusing.
    Thanks. When we started this project we were complete newbies in programming so we are basically learning from scratch. We will take care of all those conventions.

    Thanks for your time and patience helping us. ;)

  15. #55
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Can't call variable from other class

    You are welcome. Good luck on your projects.
    If you don't understand my response, don't ignore it, ask a question.

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Call a public string variable
    By onstock in forum New To Java
    Replies: 3
    Last Post: 11-23-2011, 12:25 PM
  2. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  3. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  4. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  5. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •