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 3433394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:34:06+00:00 2026-05-18T07:34:06+00:00

I basically have: Public Class Job: MyBaseClass { public virtual string JobInformation {get;set;} …

  • 0

I basically have:

Public Class Job: MyBaseClass
{
    public virtual string JobInformation {get;set;}
    ...
    public virtual List<Item> JobItems {get;set;}
}

I was using a List and initializing it (JobItems = new List();) in the constructor. However, I was getting an Exception (Nhibernate.Collection.Generic.PersistentGenericBag)

I read this question and it said I should use IList instead.

So now I have

public class Job: MyBaseClass
{
    public virtual string JobInformation {get;set;}
    ...
    public virtual IList<Item> JobItems {get;set;}
}

public virtual void AddItem(Item item)
{
     //snip validation
     this.JobItems.Add(item);
}

However, it throws a NullReferenceException because JobItems isn’t initialized yet. When does NHibernate initialize this collection? Or how can I solve this issue?

  • 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-05-18T07:34:07+00:00Added an answer on May 18, 2026 at 7:34 am

    use a backing field and on return make sure it isn’t null

    public IList<Item> JobItems
    {
        get { return _jobItems ?? (_jobItems = new List<Item>()); }
        set { _jobItems = value; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have basically this mapping: @Entity @Table(schema=xas,name=billing) public class Billing implements Serializable{ private
Say I have a component like this public class MyComponent { public MyComponent(string name)
I have a WCF service which basically returns [DataContract(IsReference = true)] public class Person
Basically I have the following class: class StateMachine { ... StateMethod stateA(); StateMethod stateB();
I have the code below : public class Anything { public int Data {
basically, i want to have something like: class DataProcessor{ }; however, in the future,
So I have this class public static class MyClass { static MyClass() { ...
public class Main { public static void main(String[] args) { int[] x = {1,
Basically I have a class A which creates an array upon construction. class A
I basically have a page which shows a processing screen which has been flushed

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.