-
February 3rd, 2010, 04:25 AM
#1
[RESOLVED] A list in a list
Can anyone please tell me what this error means and how to fix it
Products.Add(new product());
Products[0].Code = "ANW135";
Products[0].Description = "Coffee Mug";
Products[0].Batch.Add(new batch());
// The above line has the following error when I run the program
// Error = System.NullReferenceException was unhandled
// Message="Object reference not set to an instance of an object."
class product
{
public String Code;
public String Description;
public List<batch> Batch;
}
class batch
{
public Int64 Cost;
public Int64 Rebate;
public Int64 Freight;
}
-
February 3rd, 2010, 04:28 AM
#2
Re: A list in a list
Batch is a List of batch objects. Being a list, it's a reference type. You must allocate memory for it, otherwise it's just a null reference, hence your error.
Code:
Products.Add(new product());
Products[0].Code = "ANW135";
Products[0].Description = "Coffee Mug";
Products[0].Batch = new List<batch>();
Products[0].Batch.Add(new batch());
What you can do is providing a constructor for product that creates the list.
Code:
class product
{
public String Code;
public String Description;
public List<batch> Batch;
public product()
{
Batch = new List<batch>();
}
}
BTW, you're not following the .NET naming conventions, which you should.
-
February 3rd, 2010, 04:32 AM
#3
Re: A list in a list
Nevermind, I just figured it out
-
February 3rd, 2010, 04:33 AM
#4
Re: A list in a list
Then you should mention than and marked the threads resolved so that we don't waste the time providing answers that you already have.
-
February 3rd, 2010, 04:37 AM
#5
Re: A list in a list
I was just doing that when you were posting your comment. Thankyou very much for your help. When you say that I'm not following the naming conventions, is that because I am using the same name for the class type and the list, but in different case?
I'm sorry for the stupid questions but I am very new
-
February 3rd, 2010, 04:42 AM
#6
Re: A list in a list
It's not a stupid question. Yes, the name for your classes start with lower letters. That is because the name of the property collides with the name of the class. But you should name the property Batches instead, because it's a list of batches, not a single batch.
Code:
class Product
{
public String Code;
public String Description;
public List<Batch> Batches;
}
class Batch
{
public Int64 Cost;
public Int64 Rebate;
public Int64 Freight;
}
See this article for .NET naming conventions: http://msdn.microsoft.com/en-us/libr...8VS.71%29.aspx.
-
February 3rd, 2010, 04:52 AM
#7
Re: A list in a list
Thankyou, your help is really apreciated
-
February 3rd, 2010, 02:13 PM
#8
Re: A list in a list
Originally Posted by cilu
Then you should mention than and marked the threads resolved so that we don't waste the time providing answers that you already have.
or so that we know we can find an answer there. it would be so cool if everyone did that
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
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
|