如果没有选择文件,如何插入错误信息? [英] How do I insert error message if no file is selected?
问题描述
我尝试过其他功能,它可以工作,但对于这个SELECT选项,它不起作用。
这是我的代码: p>
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(fileToPath == null)
{
JOptionPane.showMessageDialog(null,请选择1个文件!,错误,JOptionPane.ERROR_MESSAGE);
}
}
});
但是,我没有看到预期的错误消息。
线程中的异常AWT-EventQueue-0java.lang.NullPointerException
在波动率$ 3.actionPerformed(Volatility.java:204)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)
在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)
在javax.swing.DefaultButtonModel.setPressed(未知源)
在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知源)
在java.awt.Component.processMouseEvent(未知来源)
在javax.swing.JComponent.processMouseEvent(未知源)
在java.awt.Component.processEvent(未知源)
在java.awt.Container.processEvent(未知源)
在java.awt.Component.dispatchEventImpl(未知源)
在java.awt.Container.di spatchEventImpl(未知源)
在java.awt.Component.dispatchEvent(未知源)
在java.awt.LightweightDispatcher.retargetMouseEvent(未知源)
在java.awt.LightweightDispatcher.processMouseEvent(未知源)
在java.awt.LightweightDispatcher.dispatchEvent(未知源)
在java.awt.Container.dispatchEventImpl(未知来源)
...
我该如何解决?需要帮助。感谢提前。
JFileChooser#getSelectedFile
将返回 null
如果没有选择,那么基本上你正在做一些类似...
null.getAbsolutePath();
显然,可以做到,而应该先检查返回值的状态,例如...
文件selectedFile = chooser.getSelectedFile();
//如果没有选择文件,显示错误消息
if(selectedFile == null)
{
JOptionPane.showMessageDialog(null,请选择1个文件!,错误,JOptionPane.ERROR_MESSAGE);
} else {
String fileToPath = selectedFile.getAbsolutePath();
}
I am doing java. I have this "SELECT" option whereby the user will choose a file. Then "START" option to use the selected file for some purposes but how do I make sure that if the file is empty and when the user clicked "START", it will prompt error message?
I tried for other functions and it works, but for this "SELECT" option, it just doesn't work.
Here's my code:
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);
}
}
});
However, instead of seeing the expected error message, I got this.
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)
...
How can I solve this??? Help needed. Thanks in advance.
JFileChooser#getSelectedFile
will return null
if nothing is selected, so bascially you are currently doing something like...
null.getAbsolutePath();
Which, obviously, can be done, instead, you should be checking the state of the return value first, for example...
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();
}
这篇关于如果没有选择文件,如何插入错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!