1

I'm trying to rename file or directory using JFileChooser() and JButton(). But it gives me a "NullPointer exeption" in line 81 where I have action listener

        // 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");
                }
            }
        });

I have to select some file or directory from my FileChooser(), then write new name for file or directory in TextField() and then push the Button() to rename it.Can you say where I go wrong and how to solve this problem.

Exeption occurs when I push the button

This is a full code:

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");
                }
            }
        });

    }
}
Luchnik
  • 1,067
  • 4
  • 13
  • 31
  • 1
    Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 30 '15 at 14:41
  • [crossposted](https://www.daniweb.com/software-development/java/threads/493903/rename-filedirectory-using-jfilechooser-and-jbutton) – mKorbel Mar 30 '15 at 15:47
  • @mKorbel I hope that this is allowed – Luchnik Mar 30 '15 at 15:52

1 Answers1

0

It's because you're passing null to the File constructor at line 81, which give a NullPointerException when passed null. This can be read in the documentation here.

I think the solution is to remove the 'new File' part and replace it by:

File oldFile = fileChooser.getSelectedFile();

The bug was actually quite easy to see by pasting the code into IntelliJ:

IntelliJ screenshot

Erik Pragt
  • 13,513
  • 11
  • 58
  • 64
  • Yes, my exeption's gone when I pass these parameters: **File oldFile = new File(fileChooser.getSelectedFile(), newFileName);** but **oldFile.renameTo(newFileOrDirectoryName)** returns false. And I don't know where I go wrong – Luchnik Mar 30 '15 at 14:03
  • According to your suggestions, the **if statement** gives **NullPointer**. Something's wrong with this: **oldFile.renameTo(newFileOrDirectoryName)** – Luchnik Mar 30 '15 at 14:13
  • No, it doesn't. The arrow points to the `null`, not to the if statement. – Erik Pragt Mar 30 '15 at 20:17