[RESOLVED] A list in a list
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] A list in a list

  1. #1
    Join Date
    Feb 2010
    Posts
    13

    [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;
    }

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Feb 2010
    Posts
    13

    Re: A list in a list

    Nevermind, I just figured it out

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Feb 2010
    Posts
    13

    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

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Feb 2010
    Posts
    13

    Re: A list in a list

    Thankyou, your help is really apreciated

  8. #8
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: A list in a list

    Quote Originally Posted by cilu View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center