How to get values from text fields and use them in another class?












0















I have a GUI that allows a user to enter their personal information. I want to get all that information and use it in another class to create an instance of that person. I keep getting




non-static method getValues(String, String...) cannot be referenced from a static context




even though none of my methods are static.



I have tried to use individual getters for each text field, but that gave the same error. I also tried to get each value through one method and then call it in the other class but that doesn't work either.



In this class I am trying to get the values from the GUI:



public class PatientStrategy implements IAccountStrategy {

@Override
public void createAccount(String accountType, String firstname, String lastname,
String address, String postcode, String sex, Integer age){
PMSGUI.getValues(accountType, firstname, lastname, address, postcode, sex, age);
}


And this is the method I'm using in the GUI itself to get the values from the text fields:



public class PMSGUI extends javax.swing.JFrame {

public void getValues(String accountType, String firstname, String lastname, String address,
String postcode, String sex, Integer age) {
accountType = cboAccountType.getSelectedItem().toString();
firstname = txtFirstName.getText();
lastname = txtLastName.getText();
address = txtAddress.getText();
postcode = txtPostcode.getText();
sex = cboSex.getSelectedItem().toString();
age = Integer.parseInt(txtAge.getText());
}


I am using an observer and strategy pattern and so I will be taking the values from the GUI and will be creating a new instance of each person as an observer.



I'm new to programming in Java and so I know I'm probably going about this all wrong. Any help is appreciated!










share|improve this question

























  • Using PMSGUI.getValues is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.

    – RealSkeptic
    Jan 1 at 17:25











  • Thanks for the reply. What do you mean by create an instance of the method exactly?

    – Top Lit
    Jan 1 at 17:29











  • Create an instance of the class, not of the method. Use new PMSGUI() somewhere. I'm sure you already have an instance or the GUI would not be shown.

    – RealSkeptic
    Jan 1 at 17:32













  • Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.

    – Top Lit
    Jan 1 at 17:35











  • You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.

    – RealSkeptic
    Jan 1 at 17:41
















0















I have a GUI that allows a user to enter their personal information. I want to get all that information and use it in another class to create an instance of that person. I keep getting




non-static method getValues(String, String...) cannot be referenced from a static context




even though none of my methods are static.



I have tried to use individual getters for each text field, but that gave the same error. I also tried to get each value through one method and then call it in the other class but that doesn't work either.



In this class I am trying to get the values from the GUI:



public class PatientStrategy implements IAccountStrategy {

@Override
public void createAccount(String accountType, String firstname, String lastname,
String address, String postcode, String sex, Integer age){
PMSGUI.getValues(accountType, firstname, lastname, address, postcode, sex, age);
}


And this is the method I'm using in the GUI itself to get the values from the text fields:



public class PMSGUI extends javax.swing.JFrame {

public void getValues(String accountType, String firstname, String lastname, String address,
String postcode, String sex, Integer age) {
accountType = cboAccountType.getSelectedItem().toString();
firstname = txtFirstName.getText();
lastname = txtLastName.getText();
address = txtAddress.getText();
postcode = txtPostcode.getText();
sex = cboSex.getSelectedItem().toString();
age = Integer.parseInt(txtAge.getText());
}


I am using an observer and strategy pattern and so I will be taking the values from the GUI and will be creating a new instance of each person as an observer.



I'm new to programming in Java and so I know I'm probably going about this all wrong. Any help is appreciated!










share|improve this question

























  • Using PMSGUI.getValues is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.

    – RealSkeptic
    Jan 1 at 17:25











  • Thanks for the reply. What do you mean by create an instance of the method exactly?

    – Top Lit
    Jan 1 at 17:29











  • Create an instance of the class, not of the method. Use new PMSGUI() somewhere. I'm sure you already have an instance or the GUI would not be shown.

    – RealSkeptic
    Jan 1 at 17:32













  • Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.

    – Top Lit
    Jan 1 at 17:35











  • You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.

    – RealSkeptic
    Jan 1 at 17:41














0












0








0








I have a GUI that allows a user to enter their personal information. I want to get all that information and use it in another class to create an instance of that person. I keep getting




non-static method getValues(String, String...) cannot be referenced from a static context




even though none of my methods are static.



I have tried to use individual getters for each text field, but that gave the same error. I also tried to get each value through one method and then call it in the other class but that doesn't work either.



In this class I am trying to get the values from the GUI:



public class PatientStrategy implements IAccountStrategy {

@Override
public void createAccount(String accountType, String firstname, String lastname,
String address, String postcode, String sex, Integer age){
PMSGUI.getValues(accountType, firstname, lastname, address, postcode, sex, age);
}


And this is the method I'm using in the GUI itself to get the values from the text fields:



public class PMSGUI extends javax.swing.JFrame {

public void getValues(String accountType, String firstname, String lastname, String address,
String postcode, String sex, Integer age) {
accountType = cboAccountType.getSelectedItem().toString();
firstname = txtFirstName.getText();
lastname = txtLastName.getText();
address = txtAddress.getText();
postcode = txtPostcode.getText();
sex = cboSex.getSelectedItem().toString();
age = Integer.parseInt(txtAge.getText());
}


I am using an observer and strategy pattern and so I will be taking the values from the GUI and will be creating a new instance of each person as an observer.



I'm new to programming in Java and so I know I'm probably going about this all wrong. Any help is appreciated!










share|improve this question
















I have a GUI that allows a user to enter their personal information. I want to get all that information and use it in another class to create an instance of that person. I keep getting




non-static method getValues(String, String...) cannot be referenced from a static context




even though none of my methods are static.



I have tried to use individual getters for each text field, but that gave the same error. I also tried to get each value through one method and then call it in the other class but that doesn't work either.



In this class I am trying to get the values from the GUI:



public class PatientStrategy implements IAccountStrategy {

@Override
public void createAccount(String accountType, String firstname, String lastname,
String address, String postcode, String sex, Integer age){
PMSGUI.getValues(accountType, firstname, lastname, address, postcode, sex, age);
}


And this is the method I'm using in the GUI itself to get the values from the text fields:



public class PMSGUI extends javax.swing.JFrame {

public void getValues(String accountType, String firstname, String lastname, String address,
String postcode, String sex, Integer age) {
accountType = cboAccountType.getSelectedItem().toString();
firstname = txtFirstName.getText();
lastname = txtLastName.getText();
address = txtAddress.getText();
postcode = txtPostcode.getText();
sex = cboSex.getSelectedItem().toString();
age = Integer.parseInt(txtAge.getText());
}


I am using an observer and strategy pattern and so I will be taking the values from the GUI and will be creating a new instance of each person as an observer.



I'm new to programming in Java and so I know I'm probably going about this all wrong. Any help is appreciated!







java swing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 19:18









Kод it

2,67342240




2,67342240










asked Jan 1 at 17:18









Top LitTop Lit

327




327













  • Using PMSGUI.getValues is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.

    – RealSkeptic
    Jan 1 at 17:25











  • Thanks for the reply. What do you mean by create an instance of the method exactly?

    – Top Lit
    Jan 1 at 17:29











  • Create an instance of the class, not of the method. Use new PMSGUI() somewhere. I'm sure you already have an instance or the GUI would not be shown.

    – RealSkeptic
    Jan 1 at 17:32













  • Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.

    – Top Lit
    Jan 1 at 17:35











  • You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.

    – RealSkeptic
    Jan 1 at 17:41



















  • Using PMSGUI.getValues is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.

    – RealSkeptic
    Jan 1 at 17:25











  • Thanks for the reply. What do you mean by create an instance of the method exactly?

    – Top Lit
    Jan 1 at 17:29











  • Create an instance of the class, not of the method. Use new PMSGUI() somewhere. I'm sure you already have an instance or the GUI would not be shown.

    – RealSkeptic
    Jan 1 at 17:32













  • Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.

    – Top Lit
    Jan 1 at 17:35











  • You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.

    – RealSkeptic
    Jan 1 at 17:41

















Using PMSGUI.getValues is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.

– RealSkeptic
Jan 1 at 17:25





Using PMSGUI.getValues is exactly where you are trying to access a non-static method in a static context. To access a method, you have to create an instance and use the method with that instance.

– RealSkeptic
Jan 1 at 17:25













Thanks for the reply. What do you mean by create an instance of the method exactly?

– Top Lit
Jan 1 at 17:29





Thanks for the reply. What do you mean by create an instance of the method exactly?

– Top Lit
Jan 1 at 17:29













Create an instance of the class, not of the method. Use new PMSGUI() somewhere. I'm sure you already have an instance or the GUI would not be shown.

– RealSkeptic
Jan 1 at 17:32







Create an instance of the class, not of the method. Use new PMSGUI() somewhere. I'm sure you already have an instance or the GUI would not be shown.

– RealSkeptic
Jan 1 at 17:32















Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.

– Top Lit
Jan 1 at 17:35





Oh I see. Would creating a new instance of the GUI reset all the values in the text field then, or does it create an instance while keeping the inputted values? Sorry if I'm looking at this the wrong way.

– Top Lit
Jan 1 at 17:35













You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.

– RealSkeptic
Jan 1 at 17:41





You are supposed to use the same instance that you are using already, otherwise you'll have two GUIs appear.

– RealSkeptic
Jan 1 at 17:41












1 Answer
1






active

oldest

votes


















1














The error message tells you that getValues(), a non-static method, needs a non-static context. This context is provided by any instance of the defining class. An instantiation is a construction from a "static" class into a "dynamic" object (this object may only exist, while your program is running). Since one class can have many such objects, created from one class, each of them is called an instance.



Now if there should be at most one such window at a time, then you can apply the Singleton pattern to your GUI class. Then you would be able to access all non-static methods through the provided static instance of the class.



public class PMSGUI extends javax.swing.JFrame {
private static final PMSGUI instance = new PMSGUI();

private PMSGUI() {} // Restrict instantiation

public static PMSGUI getInstance() {
return instance;
}

// Add your custom methods further down here
}


Now to reference it in your calling method, use



PMSGUI.getInstance().getValues(accountType, firstname, lastname, address, postcode, sex, age);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997428%2fhow-to-get-values-from-text-fields-and-use-them-in-another-class%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The error message tells you that getValues(), a non-static method, needs a non-static context. This context is provided by any instance of the defining class. An instantiation is a construction from a "static" class into a "dynamic" object (this object may only exist, while your program is running). Since one class can have many such objects, created from one class, each of them is called an instance.



    Now if there should be at most one such window at a time, then you can apply the Singleton pattern to your GUI class. Then you would be able to access all non-static methods through the provided static instance of the class.



    public class PMSGUI extends javax.swing.JFrame {
    private static final PMSGUI instance = new PMSGUI();

    private PMSGUI() {} // Restrict instantiation

    public static PMSGUI getInstance() {
    return instance;
    }

    // Add your custom methods further down here
    }


    Now to reference it in your calling method, use



    PMSGUI.getInstance().getValues(accountType, firstname, lastname, address, postcode, sex, age);





    share|improve this answer




























      1














      The error message tells you that getValues(), a non-static method, needs a non-static context. This context is provided by any instance of the defining class. An instantiation is a construction from a "static" class into a "dynamic" object (this object may only exist, while your program is running). Since one class can have many such objects, created from one class, each of them is called an instance.



      Now if there should be at most one such window at a time, then you can apply the Singleton pattern to your GUI class. Then you would be able to access all non-static methods through the provided static instance of the class.



      public class PMSGUI extends javax.swing.JFrame {
      private static final PMSGUI instance = new PMSGUI();

      private PMSGUI() {} // Restrict instantiation

      public static PMSGUI getInstance() {
      return instance;
      }

      // Add your custom methods further down here
      }


      Now to reference it in your calling method, use



      PMSGUI.getInstance().getValues(accountType, firstname, lastname, address, postcode, sex, age);





      share|improve this answer


























        1












        1








        1







        The error message tells you that getValues(), a non-static method, needs a non-static context. This context is provided by any instance of the defining class. An instantiation is a construction from a "static" class into a "dynamic" object (this object may only exist, while your program is running). Since one class can have many such objects, created from one class, each of them is called an instance.



        Now if there should be at most one such window at a time, then you can apply the Singleton pattern to your GUI class. Then you would be able to access all non-static methods through the provided static instance of the class.



        public class PMSGUI extends javax.swing.JFrame {
        private static final PMSGUI instance = new PMSGUI();

        private PMSGUI() {} // Restrict instantiation

        public static PMSGUI getInstance() {
        return instance;
        }

        // Add your custom methods further down here
        }


        Now to reference it in your calling method, use



        PMSGUI.getInstance().getValues(accountType, firstname, lastname, address, postcode, sex, age);





        share|improve this answer













        The error message tells you that getValues(), a non-static method, needs a non-static context. This context is provided by any instance of the defining class. An instantiation is a construction from a "static" class into a "dynamic" object (this object may only exist, while your program is running). Since one class can have many such objects, created from one class, each of them is called an instance.



        Now if there should be at most one such window at a time, then you can apply the Singleton pattern to your GUI class. Then you would be able to access all non-static methods through the provided static instance of the class.



        public class PMSGUI extends javax.swing.JFrame {
        private static final PMSGUI instance = new PMSGUI();

        private PMSGUI() {} // Restrict instantiation

        public static PMSGUI getInstance() {
        return instance;
        }

        // Add your custom methods further down here
        }


        Now to reference it in your calling method, use



        PMSGUI.getInstance().getValues(accountType, firstname, lastname, address, postcode, sex, age);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 19:44









        Kод itKод it

        2,67342240




        2,67342240
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997428%2fhow-to-get-values-from-text-fields-and-use-them-in-another-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith