使用JFileChooser()和JButton()重命名文件/目录

时间:2015-03-30 13:50:17

标签: java swing file nullpointerexception jfilechooser

我尝试使用JFileChooser()和JButton()重命名文件或目录。但它给了我一个" NullPointer exeption "在我所拥有的第81行 动作监听器

        // button action
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String newFileName = textField.getText();

                // exeption is here!!!
                File oldFile = new File(fileChooser.getSelectedFile(), null);

                File newFileOrDirectoryName = new File(newFileName);
                if (oldFile.renameTo(newFileOrDirectoryName)) {
                    System.out.println("renamed");
                } else {
                    System.out.println("Error");
                }
            }
        });

我必须从 FileChooser()中选择一些文件或目录,然后在 TextField()中为文件或目录写新名称,然后按按钮()重命名它。你能说出错的地方以及如何解决这个问题。

Exeption occurs when I push the button

这是一个完整的代码:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;

public class App {

    private JFrame frame;
    private JTextField textField;
    private JFileChooser fileChooser;

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    App window = new App();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public App() {
        initialize();
    }

    public void initialize() {
        frame = new JFrame();
        frame.getContentPane().setFont(new Font("Tahoma", Font.BOLD, 14));
        frame.setBounds(100, 100, 539, 469);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.getContentPane().setBackground(new Color(255, 246, 143));

        JLabel label = new JLabel("Rename file or package");
        label.setFont(new Font("Tahoma", Font.BOLD, 14));
        label.setHorizontalAlignment(SwingConstants.LEFT);
        label.setBounds(10, 0, 302, 32);
        frame.getContentPane().add(label);

        JLabel label_1 = new JLabel("New file/package name");
        label_1.setHorizontalAlignment(SwingConstants.LEFT);
        label_1.setFont(new Font("Tahoma", Font.BOLD, 14));
        label_1.setBounds(10, 358, 194, 25);
        frame.getContentPane().add(label_1);

        textField = new JTextField();
        textField.setBounds(10, 387, 179, 32);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        JButton button = new JButton("Rename");
        button.setFont(new Font("Tahoma", Font.BOLD, 14));
        button.setBounds(199, 384, 151, 34);
        button.addActionListener(null);
        frame.getContentPane().add(button);

        fileChooser = new JFileChooser();
        fileChooser.setBounds(10, 51, 505, 289);
        frame.getContentPane().add(fileChooser);

        // button action
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String newFileName = textField.getText();
                File oldFile = new File(fileChooser.getSelectedFile(), null);
                File newFileOrDirectoryName = new File(newFileName);
                if (oldFile.renameTo(newFileOrDirectoryName)) {
                    System.out.println("renamed");
                } else {
                    System.out.println("Error");
                }
            }
        });

    }
}

1 个答案:

答案 0 :(得分:0)

这是因为你将null传递给第81行的File构造函数,它在传递null时给出NullPointerException。这可以在文档here中阅读。

我认为解决方案是删除“新文件”部分并将其替换为:

File oldFile = fileChooser.getSelectedFile();

通过将代码粘贴到IntelliJ中,实际上很容易看到错误:

IntelliJ screenshot