|
-
Jun 20th, 2012, 10:29 AM
#1
Thread Starter
New Member
[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!
-
Jun 20th, 2012, 10:31 AM
#2
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.
-
Jun 20th, 2012, 10:34 AM
#3
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
 
-
Jun 20th, 2012, 10:54 AM
#4
Thread Starter
New Member
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?
-
Jun 20th, 2012, 11:44 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|