2

I'm getting an error when I create static methods and call them in another class:

public static JTextField getNameTxtField(){
    return nameTxtField;
}
public static JTextField getNewUserNameTxtField(){
    return newUserNameTxtField;
}
public static JPasswordField getNewPasswordTextField(){
    return newPasswordTxtField;
}

All the above getters are located in the MainForm class and are called in the this class:

GameLogic:

public void addToDatabase() throws SQLException {
    controller.addUserToDatabase(MainForm.getNameTxtField().getText(),MainForm.getNewUserNameTxtField().getText(), String.valueOf(MainForm.getNewPasswordTextField()) , "insert into application_user values(?, ?, ?)");
}

Why am I getting the message? I don't really understand the message so can someone explain to me?

I can't create an object like this: MainForm form = new MainForm(); in the class GameLogic because I will get a StackOverflowError.

Does the problem occur because I call the static getters in a non-static method?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Mnemonics
  • 679
  • 1
  • 10
  • 26
  • Is that a compilation error or runtime error? – kajacx Mar 22 '14 at 22:40
  • Is `nameTxtField` static? Also exactly which line is generating the error? – ApproachingDarknessFish Mar 22 '14 at 22:41
  • @ValekHalfHeart yes nameTxtField is static. I'm using intellij idea so in the beginning nameTxtField wasn't static. I changed that now. And it doesn't say anything which line the error is generated from. – Mnemonics Mar 22 '14 at 22:49
  • @kajacx it is a compile time error. The program doesn't start at all. i can't even debug it. – Mnemonics Mar 22 '14 at 22:59
  • "I don't really understand the message" - @Mnemonics. What is the message? – Whymarrh Mar 23 '14 at 00:04
  • @Whymarrh I am pretty sure that the problem is that I am not used to use Intellij Idea. It works a little bit different compared to Eclipse. To create a GUI using the gui builder you have to bind the fields to the panel. If you have used Visual Basic gui builder you will see that it generates code for you. Same thing with Intellij Idea. If you accidentally delete something from this file chaos will occur. Like it has in my case. If someone of you has used Intellij Idea i would be happy to know how to get access to the file window builder generates. Thanks. – Mnemonics Mar 23 '14 at 00:21
  • @Whymarrh The only message I get is "Cannot bind to static field "nameTxtField" – Mnemonics Mar 23 '14 at 00:23

2 Answers2

2

Forget the statics. I assume what you're trying to do is reference an instance variable from one class into another. And your attempt is to use static variables. And you're getting stackoverflow because you're attempting something like this

public class MainForm ... {
    public MainForm() {
        GameForm game = new GameForm();
    }
}
public class GameForm ... {
    public GameForm() {
        MainForm main = new MainForm();
    }
}

This will definitely give you a stackoverflow because you create a GameForm which creates MainForm, which creates a GameForm, which creates a MainForm, which creates a GameForm, which creates a MainForm (get the point?), which creates a stackoverflow.

A solution is to pass MainForm to GameForm by reference and use getter and setters in MainForm where necessary. Something like

public class MainForm ... {
    private JTextField field;
    private GameForm game;

    public MainForm() {
        game = new GameForm(MainForm.this);
    }

    public JTextField getField (){
        return field;
    }
}

public class GameField ... {
    private MainForm main;
    private JTextField field;

    public GameForm(final MainForm main) {
        this.main = main;
        this.field = main.getField();
    }
}

Now, in GameForm you are referencing the same instance JTextField.


An even better, and probably more appropriate solution though, would be to use an interface and have MainForm implement it. You can see this answer for an example.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks, it worked. Now I will go through the code so I can learn something new. – Mnemonics Mar 23 '14 at 01:29
  • Can you explain why the code above doesn't cause an Stack-overflow error? Much appreciated! – Mnemonics Mar 23 '14 at 01:35
  • Because nowhere are you creating a `new MainForm` in the `GameForm`, You are only passing a reference of the current `MainForm` to the `GameForm`. It's a _huge_ difference. – Paul Samsotha Mar 23 '14 at 01:36
  • okay thank you. I suppose game = new GameLogic(MainForm.this) refers to the current MainForm without creating one? – Mnemonics Mar 23 '14 at 01:38
0

getText() (called in addToUserDatabase()) is a non-static function. You can't reference something that's non-static from a static context.

TMBT
  • 1,183
  • 10
  • 17