나는 취소 또는 X를 누르지 않는 한 사용자가 올바른 파일 이름을 입력하도록 트랩하려고합니다. 입력을 취소하면 내 프로그램에서 NullPointerException을 발생시킵니다. . 사용자가 취소 버튼을 칠 때Java-Swing : 사용자가 올바른 파일 이름을 입력하지 못하도록 함
public void openSaveAsDirectory() {
JDialog dialog = new JDialog();
dialog.setTitle("Save file as");
String name = JOptionPane.showInputDialog(adTable, "Please type a name for your file");
if(name != null && !name.isEmpty()) {
File fileName = new File(SAVE_LOCATION + FILE_SEPERATOR + name + FILE_SUFFIX);
book.saveUser(fileName);
}
while(name.isEmpty()) {
name = JOptionPane.showInputDialog(adTable, "Please type a name for your file");
}
}
글쎄, 입력 대화 상자를 취소하면 'name'은 null이됩니다. 당신은'while (name.isEmpty()) {'를 수행합니다. 'name'은 null 일 수 있습니다. – nbokmans
[NullPointerException이란 무엇이며 어떻게 수정합니까?] (https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it)) – nbokmans
그냥 궁금 해서요. 왜 JOptionPane 대 JFileChooser를 사용하고 있습니까? – ControlAltDel