简体   繁体   中英

Why is my output null every time?

Hello I have a class that opens a JFrame and takes in a text. But when I try getting the text it says its null. Everytime I click the button I want the System.out to print the text I entered in the textArea.

This is my first class :

public class FileReader {

    FileBrowser x = new FileBrowser();
    private String filePath = x.filePath;

    public String getFilePath(){

        return this.filePath;
    }

    public static void main(String[] args) {
        FileReader x = new FileReader();
        if(x.getFilePath() == null){
            System.out.println("String is null.");
        }
        else
        {
            System.out.println(x.getFilePath());
        }
    }
}

This is a JFrame that takes in the input and stores it in a static String.

/*
 * This class is used to read the location 
 * of the file that the user.
 */
import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
import javax.swing.*;


public class FileBrowser extends JFrame{

    private JTextArea textArea;
    private JButton button;
    public static String filePath;

    public FileBrowser(){
        super("Enter file path to add");
        setLayout(new BorderLayout());

        this.textArea = new JTextArea();
        this.button = new JButton("Add file");

        add(this.textArea, BorderLayout.CENTER);
        add(this.button, BorderLayout.SOUTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 100);
        setVisible(true);

        this.button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                filePath = new String(textArea.getText());
                System.exit(0);
            }
        });

    }
}

But everytime I run these programs I get

String is null.

在此处输入图片说明

You are mistaken by the way how JFrames work. A JFrame does not stall the execution of the code until it is closed. So, basically, your code creates a JFrame and then grabs the filePath variable in that object, before the user could have possibly specified a file.

So, to solve this, move the code that outputs the filepath to stdout to the ActionListener you have. Get rid of the System.exit() call, and use dispose() instead.


Update : You should have this code for the ActionListener:

this.button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        filePath = new String(textArea.getText());

        if(filePath == null){
            System.out.println("String is null.");
        }
        else
        {
            System.out.println(filePath);
        }

        dispose();
    }
});

And as main method:

public static void main(String[] args)
{
    FileBrowser x = new FileBrowser();
}

Your main does not wait until the user has specified a text in the textArea. You could prevent this behaviour by looping until the text in the textArea is set or you could place the logic of the main function into the ActionListener to handle the event. Following the second way the main function only creates a new FileBrowser object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM