Click here to Skip to main content
15,888,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I am missing for this piece of code for it to work? Current I have an error in the inner for loop. The error is

C#
An unhandled exception of type 'System.NullReferenceException' occurred in testXMLSerializer.exe

Additional information: Object reference not set to an instance of an object.



The Code I have is like this:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Class6
    {
        static void Main (string[] args)
        {
            Author[] author = new Author[5];
            int bookCount = 3;
            for (int i = 0; i < author.Length; i++ )
            {
                for (int j = 0; j < bookCount; j++)
                {
                    author[i].book[j].Title = "book title";
                    author[i].book[j].Language = "book language";
                }
            }

                Console.Read();
        }

    }

    public class Author
    {
        public List<Book> book {get; set;} 
    }

    public class Book
    {
        public string Title {get; set;}
        public string Language {get;set;}
    }
}



Basically what I want is to have an array of Author and each author will have a List of Book. Thanks

What I have tried:

The code compile but failed during execution.
Posted
Updated 24-Jun-16 7:04am

You're code is a bit messed up. You created and array that CAN HOLD 5 Author objects. You never created these Author objects. You have to create an instance of Author and assign it to the element in the array first.
C#
Author[] author = new Author[5];
for(int i = 0...)
{
    // Create a new Author and add it to the array.
    Author newAuthor = new Author();
    author[i] = newAuthor;

    // Initialize the book collection in the new Author object.
    newAuthor.book = new List<book>();

    // Create a list of new books and add them to the Author's
    // book collection.
    for(int j = 0...)
    {
        // Create a new book...
        Book newBook = new Book();
        newBook.Title = "book title";
        newBook.Language = "book language";

        // Add it to the book collection for the current Author.
        newAuthor.book.Add(newBook);
    }
}


Really, this isn't the most efficient way of representing your data. I'm taking this as an excersize that you're trying to learn how this stuff works.
 
Share this answer
 
v3
After this line:
C#
Author[] author = new Author[5];

all elements of author are null and not an actual instance of the Author class. So the first thing to do when entering the first loop, is creating an instance of Author and putting it in the array.

Another problem is that, when creating an Author instance, the book member is also null. When an instance of Author is created, you have to assign a value to the book property. You can do this using a constructor: Constructors (C# Programming Guide)[^]

Another problem is that you try to access an element of author[i].book, but that element does not exist yet at index j. You first have to add it.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Test
{
    class Class6
    {
        static void Main (string[] args)
        {
            Author[] author = new Author[5];
            int bookCount = 3;
            for (int i = 0; i < author.Length; i++ )
            {
                author[i] = new Author();
                for (int j = 0; j < bookCount; j++)
                {
                    author[i].book.Add(new Book());
                    author[i].book[j].Title = "book title";
                    author[i].book[j].Language = "book language";
                    // or, as a one-liner: author[i].book.Add(new Book() { Title = "book title", Language = "book language" });
                }
            }
 
            Console.Read();
        }
 
    }
 
    public class Author
    {
        public List<Book> book {get; set;}

        public Author()
        {
            book = new List<Book>();
        }
    }
 
    public class Book
    {
        public string Title {get; set;}
        public string Language {get;set;}
    }
}
 
Share this answer
 
v2
I don't like working with arrays when setting new items. The Framework has the built in List<t> construct which makes doing things like this much easier. You also don't have to worry about the explicit size of your array.
C#
List<author> authors = new List<author>();
int authorCount = 5;
int bookCount = 3;
for(int i = 0; i < authorCount; i++)
{
    var newAuthor = new Author();
    for (int j = 0; j < bookCount; j++)
    {
        newAuthor.book.Add(new Book{
            Title = "book title";
            Language = "book language";
        });
    }
    authors.Add(newAuthor);
}

To access the authors list as an array all you need to call is authors.ToArray();
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900