0

I am trying to pass a simple string from a purchase class by throwing it in a getter, then trying to retrieve it from another class called pconfirm. Then trying to set the text of a label in that form to the amount being passed.

Purchase.java

private JLabel lblamnt;

public String amount() {
    String gtext = lblamnt.getText();
    return gtext;
}

Pconfirm.java

private JLabel lblgamnt;

public Pconfirm() {
    Purchase purchase = new Purchase();
    lblgamnt.setText("Test" + purchase.amount());
}

When i pass it it shows nothing.

I was under the presumption that you call it by Purchase.amount().

BTCDude
  • 31
  • 7
  • Possible duplicate [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), [Non-Static method cannot be referenced from a static context with methods and variables](https://stackoverflow.com/questions/14862306/non-static-method-cannot-be-referenced-from-a-static-context-with-methods-and-va) – MadProgrammer Jan 17 '18 at 23:45
  • `Purchase` doesn't work here because you have a class, not a static method (as the error message is saying). You need to make an instance of the class: `Purchase purchase = new Purchase(); purchase.amount();...` – markspace Jan 17 '18 at 23:46
  • *"I was under the presumption that you call it by Purchase.amount()"* - `new Purchase().amount()` would be more accurate, but, you would be wanting to use the instance of the object which was created to manage the original state – MadProgrammer Jan 17 '18 at 23:47
  • what is the name of your Purchase object? Use that – RAZ_Muh_Taz Jan 17 '18 at 23:54
  • Possible duplicate of [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Mehmet Sunkur Jan 17 '18 at 23:57
  • i did instantiate it by ==> Purchase purchase = new Purchase(); and then next line is ==> lblgamnt.setText("Test" + purchase.amount());...But when i run it and try to pass the value nothing is there. – BTCDude Jan 18 '18 at 00:02
  • @RAZ_Muh_Taz your example from below is not what i need to bring over. I need the String from lblamnt in purchase.java to be sent to pconfirm.java. Thought your example worked its just not what i was looking for. Eg. IF lblamnt contains the string "1.345" i would need that string to be sent to pconfirm and to retrieve that string to be used. – BTCDude Jan 18 '18 at 13:21
  • Seriously? Not a single answer? @RAZ_Muh_Taz – BTCDude Jan 19 '18 at 23:30

1 Answers1

0

You need to have an actual object to use the method amount()

In your Purchase class constructor hopefully you give access to the JLabel or the ability to set the text like so

Purchase.java

public void setTextOfJLabel(String text)
{
    lblamnt.setText(text);
}

Then you could do the following in

Pconfirm.java

private JLabel lblgamnt;
private Purchase purchObjName;

public Pconfirm() {
    purchObjName = new Purchase();
    purchObjName.setTextOfJLabel("The Purchase JLabel text");
    lblgamnt.setText("Pconfirm JLabel text and " + purchObjName.amount());
}

Unless you explicitly set the text before calling amount your text of the JLabel will be null. You could also do something where every Purchase JLabel text is the same until changed by the user using the method I provided and you can set a default text like this

Purchase.java

private final String defaultJLabelText = "Purchase Default Text"; 

public Purchase()
{
    lblamnt = new JLabel();
    lblamnt.setText(defaultJLabelText);
}
RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
  • the method settextofjlabel is just setting the text. i need to retrieve the actual amount of that jlabel when i click the lblamnt contains a string and i need that string to be sent to pconfirm and that string needs to be stored ina value. what yours does is just simply setting a fixed text. It doesnt pass the variable i need in the jlabel. – BTCDude Jan 18 '18 at 00:40
  • still stuck on this problem, driving me nuts. – BTCDude Jan 18 '18 at 10:52