Results 1 to 7 of 7

Thread: [2005] NullReferenceException was unhandled

  1. #1

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Angry [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?
    Dim Me As Coder

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    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

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    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.
    Dim Me As Coder

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Re: [2005] NullReferenceException was unhandled

    thanks all
    Dim Me As Coder

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width