Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 55
Like Tree1Likes

Thread: Can't call variable from other class

  1. #21
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Im using NetBeans IDE now because of the Designer helps, and I've got 2 classes:

    - 1 Class that holds a Form and another that holds another Form. The problem is, I want to pass a variable from the first class to the second class, and I don't know how to do it.

    So, the first class is called : "Champs1.java" and the second one: "CombateFrame.java"

    In both of them I've got a constructor, and I thought I should pass the variable the same way I did with the other classes. Something like:

    Java Code:
    CombateFrame.CombateFrame(String X);
    I would be putting this code in the Champs1 Class, in a button action listener, p.e. But it don't seem to be working...

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

    Default Re: Can't call variable from other class

    When I try to do this:

    Java Code:
    Champs1 Champs1Frame = new Champs1();
    in the CombateFrame it gives a strange error: "constructor Champs1 in class Champs1 cannot be applied to given types"... The hint is to create a constructor but I already have one...

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Can't call variable from other class

    What does the definition of the Champs1 class look like?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Can't call variable from other class

    Java Code:
    public class Champs1 extends javax.swing.JFrame implements ActionListener {
    
    // VARIABLES
    
    public Champs1(Main mainFrame) { 
    
    //Constructor where I have all the buttons action listeners, values given to variables, etc... 
    
    }

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Can't call variable from other class

    Where is the constructor this statement is asking for?
    Champs1 Champs1Frame = new Champs1(); //<<<<<<< NO ARGS
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Can't call variable from other class

    Ok I tried to create a "second constructor(?)" in the class Champs1:

    Java Code:
    Champs1() {
    
    }
    Am I doing right? What Arguments should I be calling?

  7. #27
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Can't call variable from other class

    That constructor does not take any arguments. It will have to assign default values to the class variables.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Can't call variable from other class

    But I've got this:

    Java Code:
    public class Champs1 extends javax.swing.JFrame implements ActionListener {
    
        //Variavéis
        Main champs1Frame;
        ChampsList ChampsList = new ChampsList();
        Combate Combate = new Combate();
        CombateFrame CombateFrame;
        public static String Champ1, Champ2;
        public double HP1, HP2, attack1, attack2, defense1, defense2;
        public int champ1Ref, champ2Ref;
        private int player = 1;
    
        Champs1() {
           
        }
    
        public Champs1(Main mainFrame) {
    
            mainFrame = champs1Frame;
    
            CombateFrame = new CombateFrame(this);
    
            initComponents(); //Desenhar frame
    
            // Ver se todos escolheram as personagens
            Thread checkThread = new Thread(new Runnable() {
    
                public void run() {
                    while (player != 0) {
                        if (player == 3) {
                            //Podemos acrescentar aqui os outros botões todos para serem desactivados
                            bContinue.setEnabled(true);
                        }
                    }
                }
            });
    
            checkThread.start();
    
            /*
             * Temos de passar todos os stats para a class de combate que começo a
             * achar que a sua única função vai ser fazer check à vitória xd Nesta
             * classe vamos declarar as personagens basicamente. Eu bem disse que
             * iamos ter de reescrever tudo isto, mas n é nada que dê demasiado
             * trabalho. :/
             */
    
            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);
                    }
    How I can pass the variables assigned by the button, like Champ1, attack1, etc to the constructor Champ1() { } ? It may look like a noob question, but I don't know how to do it. :/

  9. #29
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Can't call variable from other class

    To pass args to a constructor (or method) put them in the ()s.

    AClass aCls = new AClass(the, args, go, here);

    The constructor must be coded to accept that number and type of argument.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Can't call variable from other class

    Java Code:
    public class Champs1 extends javax.swing.JFrame implements ActionListener {
    
        //Variavéis
        Main 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(Main mainFrame) {
    
    //BUTTONS, etc.
    
    }
    Java Code:
    public class CombateFrame extends javax.swing.JFrame {
       
        Champs1 Champs1Frame = new Champs1(); // What's wrong with this line? It gives me an error: "Constructor Champs1 in class Champs1 cannot be applied to given types
    Sorry if my questions are getting a bit annoying. :/

  11. #31
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Can't call variable from other class

    Were there error messages?
    If you don't understand my response, don't ignore it, ask a question.

  12. #32
    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
    Were there error messages?
    Only on the line I said.

    Champs1 Champs1Frame = new Champs1();

  13. #33
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Can't call variable from other class

    What was the error message?
    If you don't understand my response, don't ignore it, ask a question.

  14. #34
    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
    What was the error message?
    Here: "Constructor Champs1 in class Champs1 cannot be applied to given types"

  15. #35
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Can't call variable from other class

    Please post the full text of the error message.

    Is there a constructor that has exactly matching arguments as in the new statement? Number of args and type of arg
    If you don't understand my response, don't ignore it, ask a question.

  16. #36
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Constructor Champs1 in class Champs1 cannot be applied to given types;
    required: Main
    found: no arguments
    reason: actual and formal argument lists differ in length

    I only got the:

    Java Code:
    public Champs1 (Main mainFrame) {
    
    }
    constructor in the Champs1 class. If I create a new one it messes up with other things. And if I try to call the Champs1 constructor the frame "calling" stops working...

  17. #37
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Can't call variable from other class

    If that is the only constructor, you MUST call it with one arg of type Main. A type can be the name of a class or a primitive like int.
    Java Code:
    Main aMainRef = ....  //  somewhere define a variable of type Main and give it a value
    
    
     ... = new Champs1(aMainRef);  // call the constructor with the correct type
    Last edited by Norm; 05-01-2012 at 11:30 PM.
    If you don't understand my response, don't ignore it, ask a question.

  18. #38
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    But why does he need to work with the Main class if I don't need anything from there? Because I changed the Main class name to MainMenu, just to avoid confusions and now it requires an arg of the type MainMenu...

  19. #39
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Can't call variable from other class

    If the Champs1 class does not need a reference to a Main object, add a constructor to the Champs1 class that has no arguments
    or rewrite the current constructor and remove its args
    If you don't understand my response, don't ignore it, ask a question.

  20. #40
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    But I need the args to call the frame...

    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
    
    
     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);
    
        }
    I just pasted the essencial...

    Thanks.

    PS. As you can see I need the args in the constructors, so I can call the frames I need. I can't see another way :/

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Call a public string variable
    By onstock in forum New To Java
    Replies: 3
    Last Post: 11-23-2011, 01:25 PM
  2. Replies: 0
    Last Post: 03-27-2011, 06:49 AM
  3. Replies: 5
    Last Post: 01-26-2011, 07:38 PM
  4. Replies: 2
    Last Post: 05-01-2010, 09: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, 03: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
  •