0

I have a method in Layout class which return a String from JTextField. When I call sout to check that String isn't empty, and console returns the text entered to JTextField, but when I invoke in other class the same method, the String is null.

public String name;

private void loginButtonListener(){
    loginButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if(validatePassword()) {
                menuBar.setVisible(true);
                setFundsLabel(getFunds());
                setAccWelcome("Welcome back, " + getAccountName() + "!");
                cl.show(cards,"acc");
                name = getAccountName();
                System.out.println("getNameAcc: ");
                System.out.println("-----------------");
                //System.out.println(getNameAcc());
                System.out.println("-----------------");
            }
            else
                JOptionPane.showMessageDialog(loginPanel, "Incorrect login or password", "Error", 3);
        }
    });
}
public String getNameAcc(){
    return name;
}

That's a class where the String name works.

final String SENDER_PATH = t.DATA+ File.separator+t.FUNDS+File.separator+ l.getNameAcc() +FUNDS_TXT;
    System.out.println(SENDER_PATH);

That's a extract of second class where the getNameAcc doesn't work. (this is invoke in void method by actionListener on button).

Where I have a mistake?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Emil S
  • 29
  • 4
  • Is it intended that you call a method `getAccountName()` instead of `getNameAcc()` inside of `actionPerformed()` ? – Visual Studio Jun 14 '21 at 21:42
  • `getNameAcc()` is called to check in `loginButtonListener` , the `SENDER_PATH` must contain the `l.getNameAcc()` to read special file. – Emil S Jun 14 '21 at 21:44
  • the `getAccountName()` returns `String` from JTextField. – Emil S Jun 14 '21 at 21:45
  • When I want use `getAccountName()` in `SENDER_PATH` the console returns `NPE Exception` – Emil S Jun 14 '21 at 21:46
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 14 '21 at 22:11
  • that's useless example for me, I know what I should to do when console returns `NPE Exception` , I need just know how can I send variable from first class to second, where in second the sent variable is just empty – Emil S Jun 16 '21 at 21:30
  • The question is literally *"Where I have a mistake?"* - the two links in point (1) were hopefully to both give you the knowledge needed to track the problem down and fix it, or to warn you that nobody else could solve the problem without both the stack trace ***and*** an MRE / SSCCE. I provided some tips, it is your responsibility to take advantage of those tips and progress towards the solution. Best of luck with it, I won't be spending further time commenting. – Andrew Thompson Jun 17 '21 at 00:40

0 Answers0