我该如何解决System.NullReferenceException错误 [英] how do i solve the System.NullReferenceException error

查看:442
本文介绍了我该如何解决System.NullReferenceException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在我的数据库上运行查询时,我一直收到此错误,无论是选择还是插入。我该怎么解决这个问题。





I keep getting this error anytime i try to run a query on my database be it a select or insert. how do i solve this problem.


System.NullReferenceException: Object reference not set to an instance of an object





此错误指向我的代码的第39行。代码如下所示:



This error points to line 39 of my code. the code is shown below:

String sql, connection, lname, fileloc;
           sql = "select FileLoc, Fname, Lname from apps where Lname = '"+searchString.Text+"'";
           connection = Properties.Settings.Default.cvmanagerConnectionString;

           SqlDataReader reader = null;

           SqlConnection conn = new SqlConnection(connection);
           SqlCommand cmd = new SqlCommand(sql, conn);

           try
           {
               conn.Open();
               cmd.ExecuteReader();

               intsearchLB.Enabled = true;

               while (reader.Read())// THIS IS LINE 39
               {
                   lname = reader["Lname"].ToString();
                   fileloc = reader["FileLoc"].ToString();

                   intsearchLB.Items.Add(new searchresults(lname, fileloc));
               }

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
           finally
           {
               nsCancel.Enabled = true;
               nsView.Enabled = true;
               nsEdit.Enabled = true;
               nsSave.Enabled = true;
               nsDate.Enabled = true;
               nsTime.Enabled = true;
           }

推荐答案

使用调试器找出它发生的位置。由于我们不知道您的代码是什么样的,这是您将获得的最佳建议。


Use the debugger to find out where it's happening. Since we don't know what your code looks like, that's the best advice you're going to get.


当您看到此异常时而你正在使用VS,90%的问题都解决了。在调试器下运行它 - 它会通过令人讨厌的代码行直接进入你的脸。 (你做到了吗?还有问题吗?在哪里?)



你需要有适当的Debug Exception选项:VS Main menu - >调试 - >例外...(Ctrl + D,E)。默认情况下,选项几乎是最优的。



如果它没有帮助,你需要提出更具体的问题。
As you see this exception and you're using VS, 90% of your problem is solved. Run it under debugger -- it will through the offending line of code right into your face. (Did you do this? Still a problem? Where?)

You need to have proper Debug Exception options: VS Main menu -> Debug -> Exceptions... (Ctrl+D,E). By default, the options are nearly optimal.

If it did not help, you need to ask more concrete question.


好的,更新后这个错误报告很好。



Ok, after update this bug report is good.

SqlDataReader reader = null;
//...
                while (reader.Read()) //...





来吧,你自己分配 null 并且从未构建读者您尝试读取



您可能需要





Come on, you assigned null by yourself and never constructed reader by the point where you try to Read!

You probably need

reader = cmd.ExecuteReader();


这篇关于我该如何解决System.NullReferenceException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆