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

  • Home
  • SEARCH
  • 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 8461699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:56:54+00:00 2026-06-10T13:56:54+00:00

I thought it would be an easy task, but I’m struggling for a while

  • 0

I thought it would be an easy task, but I’m struggling for a while now already.

I have Sport object which belongs to some SportGroup object.

I want to represent groups inside a combobox, and I created SportViewModel.cs with the following properties

public Guid Id {get; set;}
public string Name {get; set;}
public Guid? SportGroupId { get;set; }
public IEnumerable<SportGroup> SportGroups {get; set; }

HttpGet create action

SportViewModel newSport = new SportViewModel();
newSport.SportGroups = new SelectList(GetAllSportGroups(), "Id", "Name");

//GetAllSportGroups() returns list of SportGroups domain objects as you can imagine.

My create view looks like this

@model Models.SportViewModel
<div>Sport group</div>
<div>@Html.DropDownListFor(x => x.SportGroupId, Model.SportGroups)</div>

Now on the controller’s httpPost action I receive the data and try to save it:

[HttpPost]
public ActionResult Create(SportViewModel newSport)
{
   // if model state is valid and other checking omitted
   // session and transaction omitted
   Sport sport = new Sport();
   sport.Id = newSport.Id;
   sport.Name = newSport.Name;
   SportGroup sportGroup = session.Load<SportGroup>(SportGroupId);
   sport.SportGroups.Add(sportGroup); // here is where I'm getting an error
}

After submitting I’m getting these error

Object reference not set to an instance of an object.
Referencing line sport.SportGroups.Add(sportGroup);

Please help.
Thanks

  • 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-10T13:56:55+00:00Added an answer on June 10, 2026 at 1:56 pm

    Try this:

    // if model state is valid and other checking omitted
    // session and transaction omitted
    Sport sport = new Sport();
    
    if(sport.SportGroups != null)
    {
       // everything OK !
    }
    else
    {
       // you need to instantiate the .SportGroups property!
       sport.SportGroups = new .........();
    }
    

    Because otherwise:

    sport.SportGroups.Add(sportGroup); // here is where I'm getting an error
         *************
    

    If this .SportGroups property is NOT properly instantiated, it will be NULL and you cannot call .Add() on it! You’ll get that exact exception that you have.

    This is typically something you would best do in the constructor of the Sport class – make sure that all contained collections etc. are also initalized and instantiated when you create a new object of type Sport

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

Sidebar

Related Questions

I thought the following task would be seemingly easy to code, but I have
I have an oddly difficult task to perform. I thought it would be easy,
I would have thought that this would be an easy thing to do, but
Sounds simple, but struggling to get this working. I thought it would be easy
I thought this task is easy, but I have no clue how to realize
I'm currently having a small issue which i thought would be easy, but not,
I thought this would be fairly easy, but I'm totally baffled. I want one
I thought this would be pretty easy but I'm running into all sorts of
I thought doing a simple animation would be easy but is is taking hours
I thought this would be easy, but after scanning many pages Google hasn't come

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.