Click here to Skip to main content
15,876,991 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a Student Management System and It gives me an error "Object Reference Not set to an Instance of an Object" while updating data inside the DataGridView...Update button worked successfully before i Insert a TreeView...Ever since i inserted the TreeView..Update Button Gives an error...The error shows on the SQL Command Line...

What I have tried:

This is my code...

Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
            Dim upcom11 As New SqlCommand("Update Students SET FullName='" & ffname.Text & "',FirstName='" & firstname.Text & "',LastName='" & lastname.Text & "',Gender='" & male.Checked Or female.Checked & "',Address='" & address1.Text & "',Contact='" & contact1.Text & "',NIC='" & nic1.Text & "',Email='" & Email1.Text & "',DateOfBirth='" & DOB1.Value & "' Where StudentID = '" & Registered_Students.Registered.CurrentRow.Cells(0).Value & "' ", connection)
            If Registered_Students.Registered.SelectedRows.Count > 0 Then
            End If
        Catch ex As Exception
        End Try
Updated 20-Jul-17 20:22pm

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
Share this answer
Member 13320940 25-Jul-17 7:06am    
I don't see anything that have a NULL Value...please help me figure this Out
OriginalGriff 25-Jul-17 7:24am    
We can't - this is a runtime problem and the only way to find out is to look and find the null value, which is only null while your code is running. We can't do that for you because we can't run your code!

Check every variable, every property return value, every method return - the error is very specific about what the problem is.
Member 13320940 25-Jul-17 7:47am    
I'll try..Thanks for the tips...
OriginalGriff 25-Jul-17 7:51am    
You're welcome - sorry we can't do more, but ...
0) The error is obvious. Some reference type variable has the value null and you try to call one of its properties or methods. The result is the error you see.

1) Let's not guess about the location of the error. Examine the stack trace when the exception occurs. It will tell you on which line the error occurs.

2) Use the debugger and set a breakpoint at the line the stack trace told you. Then you must examine the values of all variables to see which one it is that has the value null.

3) If you don't check before trying to call methods or properties, you will be seeing this error quite often.

4) Just guessing: Look at the line in which you put together the SQL string. It's most probably one of the many parameters which is null. You should use SQL parameters intead of strings anyway. Even if it seems to work, this is a really dangerous practice.
Share this answer
Member 13320940 25-Jul-17 6:57am    
I don't see anything that have a NULL Value...please help me figure this Out
CodeWraith 25-Jul-17 7:11am    
Did you set a breakpoint at the line shown in the stack trace of the exception and use the debugger to examine the values of the variables?

I can't tell you any more. This information is on your computer and I can not see it. You must learn how to get it.
Member 13320940 25-Jul-17 7:44am    
yes,but i couldn't see any Null Values...
Ok...i'll try more...Thanks for the Tip
CodeWraith 25-Jul-17 8:16am    
Was it the line where you put together the sql string?
Member 13320940 25-Jul-17 10:54am    
yea absolutely! In this part to be exact...
"Where StudentID = '" & Registered_Students.Registered.CurrentRow.Cells(0).Value"
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
Object reference not set to an instance of an object!

Use the debugger to see where is the error and check the variables, 1 of them is not what is expected. The command that is supposed to set that object have failed, but you don't know until you try to use it.
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
Share this answer

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