Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8447361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:09:50+00:00 2026-06-10T10:09:50+00:00

I keep getting a null reference when I try to create a collection and

  • 0

I keep getting a null reference when I try to create a collection and at the same time add a category to it. Did I forgot something? Or am I doing it all wrong?

private static void SamepleMethod(ICollectionRepository collectionRepo)
    {
        Collection collection = new Collection { CollectionName = "Collection" };

        Category category = new Category { Collection = collection, CategoryName = "Category" };

        category.SetCollection(collection);
        collection.AddCategory(category);

        collectionRepo.Save(collection);
    }

Collection:

public class Collection
{
    public virtual int? Id { get; set; }
    public virtual string CollectionName { get; set; }
    public virtual ICollection<Category> Categories { get; set; }

    public virtual void AddCategory(Category category)
    {
        Categories.Add(category);
        category.Collection = this;
    }
}

Category:

public class Category
{
    public virtual int? Id { get; set; }
    public virtual string CategoryName { get; set; }
    public virtual Collection Collection { get; set; }

    public virtual void SetCollection(Collection collection)
    {
        Collection = collection;
        collection.Categories.Add(this);
    }
}

Collection.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               namespace="ACME.Model"
               assembly="ACME.Model">

<class name="Collection" table="Collections" >

<id name="Id" column="CollectionId" type="int" >
  <generator class="identity" />
</id>

<set name="Categories" inverse="true" cascade="all">
  <key column="CollectionId" />
  <one-to-many class="Category"></one-to-many>
</set>

<property name="CollectionName">
  <column name="CollectionName" sql-type="nvarchar(50)" />
</property>

Category.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               namespace="ACME.Model"
               assembly="ACME.Model">

<class name="Category" table="Categories" >

<id name="Id" column="CategoryId" type="int" >
  <generator class="identity" />
</id>

<many-to-one name="Collection"
             class="Collection"
             column="CollectionId"
             not-null="true" />

<property name="CategoryName">
  <column name="CategoryName" sql-type="nvarchar(50)" />
</property>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:09:52+00:00Added an answer on June 10, 2026 at 10:09 am

    you never initialized the Categories property in your Collection class, and therefore it remains at its default value of null…
    You may want to initialize it to a new collection in the constructor.

    Also, I’ve counted at least 3 different code lines where you associate a category with a collection.
    I believe once is enough 🙂 (I like it best in the AddCategory method, or in the ctor of Category).

    Another small thing- hbm mapping files are a thing of the past; nowadays it’s much more convenient (and less error-prone) to map in code.
    You can do it either using the old extension fluent nHibernate, or the new official mapping-by-code, which is also very good, but alas- lacking in documentation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting a null object reference error when im running one of my
I keep getting a null exception at the ; below. The ApiUsername & ApiPassword
i keep getting a sytax error when trying to add (function (e) { e.preventDefault;
I keep getting the following error: * #1005 - Can't create table 'classorganizer.turma' (errno:
I keep getting a Null Object Refrence Error, but can't tell why. I have
I'm getting null reference errors when looking for a quantity box control in the
For some reason i keep getting the following error when i try to set
Summary: Keep getting null response despite public data and setting callback to enable cross
I am trying to parse json from a request, but I keep getting null.
I keep getting compiler errors when I try to access flashVars in an AS3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.