我正在做java。我有这个“SELECT”选项,用户将选择一个文件。然后“START”选项将所选文件用于某些目的但如何确保如果文件为空并且当用户单击“START”时,它将提示错误消息?
我尝试过其他功能,但它有效,但对于这个“SELECT”选项,它只是不起作用。
这是我的代码:
btnSelect.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent args)
{
int i = chooser.showOpenDialog(null);
if (i == chooser.APPROVE_OPTION)
{
textField.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
});
btnStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent args)
{
String fileToPath = chooser.getSelectedFile().getAbsolutePath();
// If file is not selected, show error message
if (fileToPath == null)
{
JOptionPane.showMessageDialog(null, "PLEASE SELECT 1 FILE!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
但是,我没有看到预期的错误消息,而是得到了这个。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Volatility$3.actionPerformed(Volatility.java:204)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
...
我该如何解决这个问题?需要帮助。提前谢谢。
答案 0 :(得分:1)
JFileChooser#getSelectedFile
将返回null
,因此您目前正在做类似的事情......
null.getAbsolutePath();
显然,可以这样做,你应该先检查返回值的状态,例如...
File selectedFile = chooser.getSelectedFile();
// If file is not selected, show error message
if (selectedFile == null)
{
JOptionPane.showMessageDialog(null, "PLEASE SELECT 1 FILE!", "Error", JOptionPane.ERROR_MESSAGE);
} else {
String fileToPath = selectedFile.getAbsolutePath();
}
答案 1 :(得分:0)
您是否尝试过使用try-catch块?
try {
textField.setText(chooser.getSelectedFile().getAbsolutePath());
} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
throw new SampleException(e);
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
或者您可以抛出异常:How to throw a general exception in Java? 干杯!
答案 2 :(得分:0)
这可能不是最有效的解决方案,但绝对有效。
public static boolean isFileEmpty(File file) throws IOException {
try {
BufferedReader br = new BufferedReader(new FileReader(file));
int count = 0;
String line;
while ((line = br.readLine()) != null) {
count++;
}
if (count == 0) {
return true;
}
} catch (FileNotFoundException | NullPointerException e) {
e.printStackTrace();
return false;
}
return false;
}
基本上,这可以通过制作一个试图读取文件的文件阅读器和一个能够跟踪文件阅读器读取的行数的计数变量来实现。如果循环结束且count仍为0,则表示文件读取器未读取任何行,因此必须为空。这一切都包含在try catch中,以确保a)文件存在,b)文件参数已初始化且不为null。