Click here to Skip to main content
14,691,039 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
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


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;
           }
Posted
Updated 6-Jul-20 5:21am
v2
Comments
[no name] 11-Jan-11 11:39am
   
It could be coming from anywhere but without seeing what you are trying we can't tell.
[no name] 11-Jan-11 12:58pm
   
I have updated the question...the error points to line 39 which i have indicated. This is the while loop where the reader is executed.

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.

   
v2
Comments
[no name] 11-Jan-11 12:58pm
   
I have updated the question...the error points to line 39 which i have indicated. This is the while loop where the reader is executed.
Ok, after update this bug report is good.

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


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();
   
v4
Comments
[no name] 11-Jan-11 13:12pm
   
kindly elaborate....i dont get you
[no name] 11-Jan-11 13:27pm
   
Thanks a lot. i cant believe it escaped me..thanks a lot though
Sergey Alexandrovich Kryukov 11-Jan-11 13:29pm
   
What else to elaborate?! reader is null -- exactly as your exception text tells you: "reference not set to object instance" say exactly this. Your bug found -- just fix. Can you see it?
If you don't see it, let me ask you, do you have any idea, what's the object? class? reference? variable? If not, I don't know what to ask about.

All right, you need to assign reader reference to something non-null.
Sergey Alexandrovich Kryukov 11-Jan-11 13:33pm
   
Ok, see my update.
Sergey Alexandrovich Kryukov 11-Jan-11 13:33pm
   
All right, I see John added the same...
Sergey Alexandrovich Kryukov 11-Jan-11 13:38pm
   
@plasteezy: even though you have and answer you can use, I hate my own answer.
That's the use? I typed in what you should have done, you will use it, so what?
If you don't see what's wrong with calling a method of a null object how can it help you?
You really need to step back and learn about classes, object, variables, references...
You have to set reader to somthing before trying to use it:

reader = cmd.ExecuteReader();
   
Comments
[no name] 11-Jan-11 13:27pm
   
Thanks a lot. i cant believe it escaped me..thanks a lot though
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.
   
Comments
[no name] 11-Jan-11 12:59pm
   
I have updated the question...the error points to line 39 which i have indicated. This is the while loop where the reader is executed.
Add a try cache with null reference Exception it's effective
   
Comments
Dave Kreskowiak 6-Jul-20 10:39am
   
And the absolutely wrong thing to do if you want your code to work.

I know this is an old answer I'm replying to. I'm just tagging it bad so it's not used as a fix for that exception in the future.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900