Click here to Skip to main content
15,667,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,
I have a small problem I can not solve. I have a Datagridview with multiple columns and rows. I search with a textbox with the item number to display the complete row . Here is my code:
Dim found As Boolean = False
         'search in all the rows in my datagridview
         For Each row As DataGridViewRow In data_item.Rows
             'if the colomns NumeroItem = my textbox... found = true
           If (row.Cells.Item("NumeroItem").Value.ToString = txt_search.Text) Then
                 found = True
             End If
     Catch ex As Exception
     End Try
     If found Then
         'Filter to filt the contain of the textbox
         DataTable1.DefaultView.RowFilter = "NumeroItem LIKE '*" & txt_search.Text & "*'"
     End If

My comparison does what i want properly, when it arrives at the right value, it enter in my condition. but, he catch an error something like this: Object reference not set to an instance of an object. I do not understand why?

If there is anyone who could help me it would be really nice!

What I have tried:

I try to debug it and watch line by line, but nothing is wrong! :(
Updated 2-Oct-16 8:58am
Suvendu Shekhar Giri 1-Oct-16 22:12pm    
Is "data_item" is a gridview?
Member 12754823 2-Oct-16 12:58pm    
Yes, i edited with some comment, sorry for that!
[no name] 1-Oct-16 23:22pm    
"I do not understand why", because somewhere in your code, you are trying to use the properties or methods of an object that is null. The debugger will tell you which object. We can't debug your code for you.
Member 12754823 2-Oct-16 12:59pm    
Yeah i already try to debug it, but i have 2400 rows and 11 colomns so its a bit long to watch line by line the contains of my datagridview! ;) I was hoping that someone already had the same bug!
[no name] 2-Oct-16 13:24pm    
Yep... have the same exception all the time. I probably fix 5 or 6 before breakfast. Lots and lots of people have the same bug. It's caused by the same thing and you fix it the exact same way as everyone else.

Make sure that txt_search.Text is always have some value, either entered by you or default value[e.g. txt_search.Text = "nothing"].

Object reference not set to an instance of an object.
This usually occurs when there is data available to pass further for processing BUT system doesn't understand WHERE to pass it, unless we explicitly Take Care of it.

It's Good. You are using Try Catch & Debugger. Also use ELSE condition to track where the code fails to continue and do some Brainstorm to save much time in doing debug.

Happy Programming... ;)

Mayur Dighe
How To Code For:
Share this answer
Member 12754823 2-Oct-16 17:55pm    
Thanks you for your answer, it helped me to find my bug! I had a blank row at the end of my datagridview and needed to iterate - 2 ! Thank you again, you're awesome!
MayurDighe 3-Oct-16 2:27am    
You listened carefully. Good to hear ;).
You can do one more thing - "Accept Solution"
I try to debug it and watch line by line, but nothing is wrong! :(

You are here because you know that something is wrong, you just don't find what is wrong.

Go to debugger again
put a breakpoint in your code
open 'local variables' window

as you execute your code line by line, check the contain and type of every variable involved. you may find for example that DataTable1 is not what you expect.
Share this answer
Member 12754823 2-Oct-16 12:50pm    
Yeah i already try it! But i have 2400 rows and 11 columns in my datatable so it's a bit long to check the contain line by line,I still get my problem. I was hoping that someone already had the same problem! But thanks for your answer ppolymorphe i'll continue my research!
Patrice T 2-Oct-16 17:24pm    
First of all, tell us where the error occurs.
You use a try/catch, do you have a special reason to expect a fail in this part of your code ?
Line by line is a special mode of the debugger that execute 1 line of code and pause again, so you can check how variables have changed.

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