空引用异常openFileDialog

时间:2013-05-19 19:02:02

标签: c# nullreferenceexception

代码:

namespace bla_bla_bla

{   public delegate void pathSelected(string path);
 //...
  public partial class Form1 : Form
  {
      public pathSelected onPath;
 //...
  private void button1_Click(object sender, EventArgs e)
  {

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter ="Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
    openFileDialog1.Multiselect = true;
    openFileDialog1.ShowDialog();
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    if (openFileDialog1.SafeFileName != null)
    onPath(openFileDialog1.SafeFileName);
  }

问题:

为什么我在最后一行收到null参考例?

错误:

(原创海报应在此添加)

1 个答案:

答案 0 :(得分:0)

不完全理解你的问题,或者为什么需要委托,但在你的Button1点击事件处理程序上尝试这样的事情:

int size = -1;
DialogResult result = openFileDialog1.ShowDialog(); 
if (result == DialogResult.OK) {
    string file = openFileDialog1.FileName;
    try {
        string text = File.ReadAllText(file);
        size = text.Length;
    }
    catch (IOException) {
    }
}