0

I am trying to use a getter to retrieve some variables that the user will input into a GUI, however it gives me an "non-static method cannot be referenced from a static context" error. I have tried making my getters and setters static and that did not work. I am not sure where to go from there. Here is my code: Main:

public class creditInfoSystem {
    String name = infoGUI.getName();

    public static void main(String[] args) {
        new infoGUI();       
    }

    public void getData() {     
    }
}

Getters+Setters From GUI Class:

public void setName(String newName){  
    name = newName;    
}

public String getName(){
    return name;
}

public void setCustNum(double newCustNum){  
    custNum = newCustNum;
}

public double getCustNum(){
    return custNum;
}

public void setCreditLimit(double newCreditLimit){  
    creditLimit = newCreditLimit;
}

public double getCreditLimit(){
    return creditLimit;
}

public void setPrevBalance(double newPrevBalance){  
    prevBalance = newPrevBalance;
} 

public double getPrevBalance(){
    return prevBalance;
}

public void setCurrentPurchases(double newCurrentPurchases){  
    currentPurchases = newCurrentPurchases;
}

public double getCurrentPurchases(){
    return currentPurchases;
}

public void setPayments(double newPayments){  
    payments = newPayments; 
}

public double getPayments(){
    return payments;
}

public void setCreditsReturns(double newCreditsReturns){
    creditsReturns = newCreditsReturns;
}

public double getCreditsReturns(){
    return creditsReturns;
}

public void setLateFees(double newLateFees){
    lateFees = newLateFees;
}

public double getLateFees(){
    return lateFees;
}

I can provide more parts of the code if needed.

Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
  • To read/write data to GUI objects usually exists a method for that purpose implemented in the object's class. Please post the exact row which generates the error you wrote. – Nicolò Ghielmetti Mar 01 '18 at 20:12
  • Please indent your first block properly. Also, do you know the difference between static and nonstatic methods in Java? – Sam Craig Mar 01 '18 at 21:10
  • Improved code indention for readability – Adam Kipnis Mar 01 '18 at 22:05
  • Possible duplicate of [What is the reason behind "non-static method cannot be referenced from a static context"?](https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – takendarkk Mar 01 '18 at 22:08

2 Answers2

0

First of all, rename your classs InfoGUI and CreditInfoSystem, starting with an upper case, it is a Java convention.

Then, when you do InfoGUI.getName() you are trying to access a class method, which would have to be static. What you want to do is create an instance of InfoGUI and access it's instance method.

Also your name variable should be static because you use it in a static method, ie public static void main

public class CreditInfoSystem {

   private static String name;

   public static void main(String[] args) {
      // Create an instance of your InfoGUI class
      InfoGUI infoGuiInstance = new InfoGUI();
      // Call the getter on the instance you just created
      name = infoGuiInstance.getName();
   }

}

And the renamed InfoGUI class:

class InfoGUI {

  String name;

  public void setName(String newName) {
    name = newName;
  }

  public String getName() {
    return name;
  }
}
Bentaye
  • 9,403
  • 5
  • 32
  • 45
0

The problem is that the creditInfoSystem class has no idea which infoGUI class you are referring to. Try and make infoGUI a parameter of creditInfoSystem and retrieve the data from there.

public class creditInfoSystem {
    private InfoGUI infoGUI;

   public creditInfoSystem(InfoGUI gui){
    infoGUI = gui;
    name = infoGUI.getName();
  }

  public static void main(String[] args) {
    InfoGUI infoGUI = new infoGUI();
    CreditInfoSystem sys = new creditInfoSystem(infoGUI);

  }
}

If you are building a swing/FX GUI:

One problem I can predict is knowing when the name in the GUI is actually set. You should maybe consider making the GUI the main program where CreditInfoSystem is an attribute of the GUI. Then when a button is clicked or some other action, the input from the GUI is taken and passed to the CreditInfoSystem.

TM00
  • 1,330
  • 1
  • 14
  • 26