Hello, I try to rename file or directory using JFileChooser() and JButton(). But it gives me a NullPointer exeption.
I have FileChooser, Button and TextField where I write new name for my file or directory. Can you say where I go wrong and how to solve this problem.
// button action
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String newFileName = textField.getText();
// exeption 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");
}
}
});
This is a full code:
package ua.edu.lp;
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");
}
}
});
}
}