首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >System.ArgumentNullException类型的未处理异常发生在mscorlib.dll附加信息中:路径不能为null

System.ArgumentNullException类型的未处理异常发生在mscorlib.dll附加信息中:路径不能为null
EN

Stack Overflow用户
提问于 2014-04-11 06:24:11
回答 1查看 32.9K关注 0票数 0

当图片框中的图像为空时,但当picturebox包含图像时,系统正常工作时,当我试图将数据添加到数据库时,出现了此错误。

错误:

System.ArgumentNullException类型的未处理异常发生在mscorlib.dll中 附加信息:路径不能为空。

指出的错误如下:

fsStream =新FileStream(this.imagePath,FileMode.Open,FileAccess.Read);

下面是我使用的代码:

代码语言:javascript
运行
复制
Random _random = new Random();
        Timer _timer = new Timer();
        Wait _wait = new Wait();

        FileStream fsStream;
        BinaryReader bReader;

        byte[] imageByte = null;

        string imagePath;

        string gender;

        string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\db1.accdb";

private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Title = "Open Image";
                dlg.Filter = "JPG Files (*.jpg)|*.jpg|PNG Files (*.png)|*.png|All Files (*.*)|*.*";

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    pictureBox1.Image = new Bitmap(dlg.FileName.ToString());
                    imagePath = dlg.FileName;
                }
            }
        }


public virtual void button2_Click(object sender, EventArgs e)
        {
            fsStream = new FileStream(this.imagePath, FileMode.Open, FileAccess.Read);
            bReader = new BinaryReader(fsStream);
            imageByte = bReader.ReadBytes((int)fsStream.Length);

            if (this.pictureBox1.Image == null || this.textBox2.Text == "" || this.textBox3.Text == "" || this.textBox4.Text == "" || this.textBox5.Text == "")
            {
                System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sound.Play();

                DialogResult _dialogResult = MessageBox.Show("Cannot Submit!", "Warning", MessageBoxButtons.OK);
            }

            else if (this.label9.Visible == false)
            {
                System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sound.Play();

                DialogResult _dialogResult = MessageBox.Show("You have to check the Student ID before proceeding!", "Warning", MessageBoxButtons.OK);
            }

            else if (this.label8.Visible == true)
            {
                System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sound.Play();

                DialogResult _dialogResult = MessageBox.Show("You cannot use this Student ID. Please Get another Student ID instead!", "Warning", MessageBoxButtons.OK);
            }

            else if (this.label7.Visible == true)
            {
                System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                _sound.Play();

                DialogResult _dialogResult = MessageBox.Show("Student ID is being verified! Please wait!", "Warning", MessageBoxButtons.OK);
            }

            else
            {
                using (OleDbConnection conn = new OleDbConnection(connectionString))
                {
                    string query = "INSERT INTO [Student] ([StudentID], [FirstName], [LastName], [Photo], [DateOfBirth], [Address], [JoinedDate], [Class], [Gender]) VALUES (@StudentID, @FirstName, @LastName, @Photo, @DateOfBirth, @Address, @JoinedDate, @Class, @Gender)";

                    conn.Open();

                    using (OleDbCommand cmd = new OleDbCommand(query, conn))
                    {
                        cmd.Parameters.Add("@StudentID", System.Data.OleDb.OleDbType.VarChar);
                        cmd.Parameters["@StudentID"].Value = this.textBox1.Text;

                        cmd.Parameters.Add("@FirstName", System.Data.OleDb.OleDbType.VarChar);
                        cmd.Parameters["@FirstName"].Value = this.textBox2.Text;

                        cmd.Parameters.Add("@LastName", System.Data.OleDb.OleDbType.VarChar);
                        cmd.Parameters["@LastName"].Value = this.textBox3.Text;

                        cmd.Parameters.Add("@Photo", System.Data.OleDb.OleDbType.LongVarBinary);
                        cmd.Parameters["@Photo"].Value = imageByte;

                        cmd.Parameters.Add("@DateOfBirth", System.Data.OleDb.OleDbType.VarChar);
                        cmd.Parameters["@DateOfBirth"].Value = this.dateTimePicker1.Text;

                        cmd.Parameters.Add("@Address", System.Data.OleDb.OleDbType.VarChar);
                        cmd.Parameters["@Address"].Value = this.textBox4.Text;

                        cmd.Parameters.Add("@JoinedDate", System.Data.OleDb.OleDbType.VarChar);
                        cmd.Parameters["@JoinedDate"].Value = this.dateTimePicker2.Text;

                        cmd.Parameters.Add("@Class", System.Data.OleDb.OleDbType.VarChar);
                        cmd.Parameters["@Class"].Value = this.textBox5.Text;

                        cmd.Parameters.Add("@Gender", System.Data.OleDb.OleDbType.VarChar);
                        cmd.Parameters["@Gender"].Value = gender;

                        _wait.ShowDialog();

                        System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
                        _sound.Play();

                        DialogResult _dialogResult = MessageBox.Show("Added Successfully!", "Success", MessageBoxButtons.OK);

                        cmd.ExecuteNonQuery();
                    }

                    conn.Close();
                }
            }
        }

这是一张图片:

button1_Click用于上传照片(图片将显示在右上角) button2_Click用于提交(向数据库添加数据)

有人能帮我吗?

你的回答非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-11 06:28:26

我猜(可能是错的)错误发生在这一行上:

代码语言:javascript
运行
复制
fsStream = new FileStream(this.imagePath, FileMode.Open, FileAccess.Read);

所以您应该添加一个空检查。

代码语言:javascript
运行
复制
if (this.imagePath != null)

在这一行之前,并更正以下代码。

正如您在msdn中看到的,如果路径为null,FileStream的这个ctor将引发ArgumentNullException

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23004986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档