Results 1 to 13 of 13

Thread: dealing with NullReferenceException

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    58

    dealing with NullReferenceException

    Basically I am checking several cells in different spreadsheets to see if they contain the correct value. On one sheet I get an error saying: "NullReferenceException was unhandled"

    I checked the sheet and it seems ok (it would make check valid return false, but it seemed normal), maybe it has something do to with merged cells, but I didn't see any.

    Here is my code:

    vb.net Code:
    1. For Each WS In WB.Worksheets
    2.                 CheckValid = True
    3.                 If WS.Range("B4").Value.ToString <> "Employee Name:" Then
    4.                     CheckValid = False
    5.                 End If
    6.                 If WS.Range("H4").Value.ToString <> "Week No.:" Then
    7.                     CheckValid = False
    8.                 End If
    9.                 If WS.Range("F8").Value.ToString <> "Cost" Then
    10.                     CheckValid = False
    11.                 End If
    12.  
    13.                 ' Add the sheet to the list
    14.                 If CheckValid = True Then
    15.                     CheckedListBox1.Items.Add(WS.Name)
    16.                 End If
    17.             Next

    Just wondering how I would skip to the next sheet if this occurred.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: dealing with NullReferenceException

    Put whatever is inside the For...Next block inside a Try... Catch block. You can catch NullReferenceException in one of the catch blocks, or log error to file etc.
    This would allow you to move to next sheet incase there is some error on any one of them.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: dealing with NullReferenceException

    you could use a try...catch statement
    Code:
    'loop with worksheets
    Try
       'your code
    Catch ex as NullReferenceException
    
    Catch ex as exception
        messagebox(ex.tostring) ' any other errors will be caught here
    End Try
    Next ' so that we move to the next sheet
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

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

    Re: dealing with NullReferenceException

    Fortunate for you I haven't had my coffee yet, so I'm not alert enough for a snarky comment.

    I'm assuming that the error happens on the CheckedListBox1.Items.Add(WS.Name) line? Or does it happen on one of the other lines.?

    -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??? *

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

    Re: dealing with NullReferenceException

    pradeep, megalith - you guys posted while I was typing.... while that may work, I don't necessarily agree that just throwing error handling around something like that is the best idea. That's an example of defensive programming. Instead, it would be better to use offensive programming, determine WHY the error happens, then take precautions to prevent the error from happening. Having exceptions thrown is an expensive operation in .NET, so wouldn't it be better to prevent the exception from happening in the first place?

    -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??? *

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: dealing with NullReferenceException

    Quote Originally Posted by techgnome View Post
    pradeep, megalith - you guys posted while I was typing.... while that may work, I don't necessarily agree that just throwing error handling around something like that is the best idea. That's an example of defensive programming. Instead, it would be better to use offensive programming, determine WHY the error happens, then take precautions to prevent the error from happening. Having exceptions thrown is an expensive operation in .NET, so wouldn't it be better to prevent the exception from happening in the first place?

    -tg
    Yes! you are right 100%.
    But what I did is just what the OP asked for.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    58

    Re: dealing with NullReferenceException

    @ techgnome the error comes on the if xxx cell = yyyy lines

    thanks to pradeep, megalith

  8. #8
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: dealing with NullReferenceException

    @ techgnome, my reason was the same as pradeep and yes i 100&#37; agree too, the reason for the error is what matters not how to ignore it in the 'On error Goto Next' sense
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

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

    Re: dealing with NullReferenceException

    tisk, tisk, tisk.... the two of you should know better than that...

    dethredic - then what that tells me is that your range is off... you're probably hitting a worksheet with no data. You can't .ToString something that doesn't exist...

    Code:
    If WS.Range("B4") IsNot Nothing AndAlso WS.Range("B4").Value isNot Nothing AndAlso WS.Range("B4").Value.ToString...
    -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??? *

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    58

    Re: dealing with NullReferenceException

    Quote Originally Posted by techgnome View Post
    tisk, tisk, tisk.... the two of you should know better than that...

    dethredic - then what that tells me is that your range is off... you're probably hitting a worksheet with no data. You can't .ToString something that doesn't exist...

    Code:
    If WS.Range("B4") IsNot Nothing AndAlso WS.Range("B4").Value isNot Nothing AndAlso WS.Range("B4").Value.ToString...
    -tg
    You code doesn't work, I get the same error.

    Also I don't think the range is off because the cell B4 is clearly there and says "Employee", and that is where the error occurs. If I comment out the "B4" check, then the "H4" check give me the error, although H4 is merged with G4 so I could understand receiving an error with that cell.
    The final check "F8" does not give me an error...

    EDIT:
    I wrote a quick VBA macro for the spreadsheet:
    Code:
    Sub Test()
        MsgBox (ActiveSheet.Range("b4"))
    End Sub
    That returned "Employee"
    Last edited by dethredic; Mar 23rd, 2010 at 09:50 AM.

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

    Re: dealing with NullReferenceException

    Well, something in those lines is null. time to do some debugging. Check your values, use breakpoints and the Quick watch option to see what's what. Make sure things are what they are supposed to be.

    Or use the code in post 3 and ignore it.

    -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??? *

  12. #12
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: dealing with NullReferenceException

    sorry i can't help more, using vb with spreadsheets is not something i have needed to use yet, my reasoning is my end user may or may not have excel. however a null reference exception usually means you haven't declared the type, you say it works on other worksheets than the one which errors, maybe you could post the workbook as an attachment?
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    58

    Re: dealing with NullReferenceException

    Ok, it turns out the problem is merged cells. B4 was just a sneaky little bugger and was merged in a fashion which can only be described as "ninja like".

    I dunno if you guys know a work around for this, but if not I will jut use megalith's code from above.

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