我做的java。 我有这样的“选择”选项,由此用户会选择一个文件。 然后“开始”选项以使用为了某种目的所选择的文件,但我如何确保,如果该文件是空的,当用户点击“开始”,系统会提示错误信息?
我尝试了其他的功能和它的作品,但对于这个“选择”选项,这是行不通的。
这里是我的代码:
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)
...
我怎样才能解决这个问题??? 需要帮助。 提前致谢。