|
-
Mar 18th, 2007, 09:09 AM
#1
Thread Starter
Addicted Member
[2005] NullReferenceException was unhandled
NullReferenceException was unhandled
Object reference not set to an instance of an object.
This is the error that I am having lot !!!
I must sometimes put On error resume next to not see these errors. But it works without a problem.
It shows this code:
Code:
lblAlisNo.Text = ADGW.CurrentRow.Cells(0).FormattedValue
But I cant find anything wrong with it. I thought that that cell is empty when I try to write its value to label. But it is not empty. If it would empty it would write anything to that label with On error resume next. But it writes the value with on error resume next. How can I solve this problem?
-
Mar 18th, 2007, 09:30 AM
#2
Fanatic Member
Re: [2005] NullReferenceException was unhandled
More code will help finding where the error comes from, but my guess is that it has to do with your ADWG object if the error is on that line
-
Mar 18th, 2007, 10:52 AM
#3
Re: [2005] NullReferenceException was unhandled
1) On Error Resume Next doesn't fix errors.... it ignores errors. Not a good way to get around errors.
2) YOU really shouldn't be using OERN in .NET.... it's only still there for legasy reasons.
3) When the error does happen, if you examine the InnerText of the error message, it will tell you exactly where the error lies in that statement.
-tg
-
Mar 18th, 2007, 12:18 PM
#4
Thread Starter
Addicted Member
Re: [2005] NullReferenceException was unhandled
thanks but it is all the error text and there are some tips. ADGW is a datagridview control of vb.net.It is the common one. I takes informations and displays them in labels. OERN is maybe not good a solution but my software runs really without a problem. This error comes only in displaying information so ignoring errors wont cause other errors in runtime. This is my temporary solution.
-
Mar 18th, 2007, 05:14 PM
#5
Re: [2005] NullReferenceException was unhandled
It's a VERY risky solution. Your program is NOT running right, you just haven't encountered the real problem. Furthermore, that exception is costing you in performance. A safer, and more .NET solution, would be to use a Try...Catch block to trap the error. You could still ignore the error if you wanted to, but it still wouldn't be a good idea. In fact, using any error handling at all in this case isn't a really good idea.
This particular error is always a fairly simple one to solve, you might just have some difficulty tracking down which item has not yet been initialized. I believe that the error is less common in 2005 than in earlier versions of .NET because there are now default instances of lots of objects. If you fail to instantiate a new object, the default instance is used.
Assuming that the error happens on that line, and assuming that you are running in debug mode, you can simply look at the objects in the line (ADGW, ADGW.CurrentRow, ADWG.CurrentRow.Cells(0), and the label). Using Shift+F9, you will find that one of those three is Nothing. I would guess that it is the CurrentRow that is the issue, and my next guess would be the Cells(0).
When you figure out which one it is, you will probably find a way to improve your program.
My usual boring signature: Nothing
 
-
Mar 18th, 2007, 07:58 PM
#6
Re: [2005] NullReferenceException was unhandled
Finding NullReferenceExceptions is absolutely easy. When the exception is thrown the IDE points to the line that threw it. You simply select each reference on that line, right-click it and select Quick Watch. keep doing that until the Quick watch window tells you that something is Nothing. That's your null reference. Now just look back through your code to where you expected an object to be assigned to that reference. Done.
The VS 2005 is VERY sophisticated so make use of it. Mouse over everything and do lot's of right-clicking to see what can be done. Also, noone ever does but absolutely EVERYONE should go to MSDN and read about debugging because so many people consider the sum total of debugging to be staring at their code to see if something looks wrong. Just like a mechanic fixing a car, the only way to solve most issues is to run it and watch it in action. That's what debugging is and the VS IDE provides numerous tools to help you do that.
-
Mar 19th, 2007, 07:20 PM
#7
Thread Starter
Addicted Member
Re: [2005] NullReferenceException was unhandled
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|