Results 1 to 5 of 5

Thread: [RESOLVED] NullReferenceException error adding object to a list

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    9

    Resolved [RESOLVED] NullReferenceException error adding object to a list

    Hey Guys!

    I'm trying to add objects from a class I created using data from an excel worksheet to a list using a for loop

    Code:
    Dim someParts As New List(Of Parts)
    Dim excel As New Excel.Application()
    Dim wb As Excel.Workbook = excel.Workbooks.Open(OpenFileDialog1.FileName())
    Dim ws As Excel.Worksheet = TryCast(excel.ActiveSheet, Excel.Worksheet)
    Dim range As Excel.Range
    
    range = ws.UsedRange
    
    For row = 2 To range.Rows.Count
            someParts.Add(New Parts(ws.Cells(row, 3).Value2.ToString,
                                    ws.Cells(row, 2).Value2.ToString,
                                    Split(ws.Cells(row, 6).Value2.ToString, "<>"),
                                    ws.Cells(row, 4).Value2))
    Next
    When I run it I receive "System.NullReferenceException was unhandled by user code Message=Object reference not set to an instance of an object." Any help or suggestions would be great!

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: NullReferenceException error adding object to a list

    The most likely cause is your .ToString calls on the Excel cell's value. If this value is nothing, then you will get an exception. You will need to check if the return value of the .Value2 calls are nothing before you call .ToString on them.

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

    Re: NullReferenceException error adding object to a list

    When you get that error, the first thing to do is to examine the line where the error occurs. One of the objects on the line will be Nothing, and that is what you are looking for. In your case, I assume that the line is the someParts.Add line, in which case there are LOTS of potential candidates. We can rule out someParts itself, since you properly create that, so the problem is almost certainly one of the cells containing Nothing. Value2 is a method that returns something. If what it returns is Nothing, then the .ToString call will be called on Nothing, which will throw the exception, since Nothing doesn't have a .ToString method.

    That's the most likely problem, but examining each object could show something different. For instance, ws could be Nothing, since TryCast may fail.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2012
    Posts
    9

    Re: NullReferenceException error adding object to a list

    That was exactly the problem. .ToString is throwing the exception when it is handed the value of an empty cell. Is there an elegant way to check the returned values before calling .ToString?

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

    Re: NullReferenceException error adding object to a list

    No. There isn't a solution that is quite elegant enough that you could just add in a couple characters to what you have there and it would work. However, it appears that Parts is a class you created, so what you could do is add a second constructor that took arguments of type Object (it appears that most of your arguments to the current constructor are type string, which is why you convert the values to strings). In this new constructor, you could check each argument to see if it was Nothing. By doing that, you would be able to remove the .ToString call from all of the cells and just pass the values. As long as the constructor was written so that it could handle Nothing, and would convert to a string if possible, then the calling code that you have written would be a bit shorter, and the Part class would be a bit more robust.
    My usual boring signature: Nothing

Tags for this Thread

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