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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:44:37+00:00 2026-05-20T23:44:37+00:00

Sorry for the vague title. I’ll explain what I’m trying to get working. My

  • 0

Sorry for the vague title. I’ll explain what I’m trying to get working.

My Admin View uses an Admin ViewModel which in turn calls an Admin Service which is a collection of Admin Repositories. For the sake of simplicity lets say that there is only one repository referenced by the Admin Service.

When constructing my Admin View Model the top of my class looks like this

public class adminMenuVM
{
    private readonly IAdminMenuService menuService;

    public adminMenuVM(IAdminMenuService adminMenuService)
    {
        this.menuService = adminMenuService;
    }

    public adminMenuVM()
    {
        menuItems = getMenuItems();
        menuCats = getMenuCats();
    }
}

Where getMenuItems and getMenuCats are two methods that use the service to retrieve data which is then set for the view model.

My problem is that when I run the app I get the error “Object Reference not set to instance of an object”. Now I know that this is because the adminMenuVM() is called by the viwModel and not the constructor initializing the service.

My question is how can I ensure that the service constructor is called and does its thing when my view model is called?

I’ve tried using :this on the parameterless constructor but it wont allow me to assign an interface since you can’t create a new instance of an interface.

Edit @ 18:49
I’ve had a thought but don’t know how to implement it. Can anyone advise on how I could “Constructor Chain” or if it is possible? My thought is to chain the parameterless constructor to service constructor this ensuring it gets called. I may be way off but would be grateful for yer help.

  • 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-20T23:44:38+00:00Added an answer on May 20, 2026 at 11:44 pm

    Managed to figure this one out. Its not the cleanest solution but I can’t see any other way to do it. I’ll stick this in to help anyone else who comes across this issue.

    The issue boiled down to passing the current instance of the service interface to the ViewModel. The only place I could find that the instance was being set was the constructor of my controller. So very simply I declared a public global variable of the interface service type and saved the instance to that. The current instance persits and can be passed to the constructor of my viewmodel and satisfy the viewmodel constructors requirement. To clarify I’ve included an example of the controller and a viewmodel.

    Controller:

    public class AdminController : Controller
    {
        private readonly IAdminMenuService lmService;
        public IAdminMenuService testService;
    
        public AdminController(IAdminMenuService layoutMarkupService)
        {
            this.lmService = layoutMarkupService;
            testService = lmService;
        }
    
        public ActionResult Index()
        {
            return View();
        }
    
        public PartialViewResult menuPartial()
        {
            return PartialView("_AdminMenuPartial", new adminMenuVM(testService));
        }
    

    ViewModel:

    public class adminMenuVM
    {
        private readonly IAdminMenuService menuService;
    
        public adminMenuVM(IAdminMenuService AdminMenuService)
        {
            this.menuService = AdminMenuService;
            menuItems = getMenuItems();
            menuCats = getMenuCats();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the vague title, as I really can't explain this problem succinctly. Basically
Sorry about a vague title, but here's what i am trying to do. I
Sorry for such a vague title. Basically, I am trying to hack a function
Sorry for the vague title, but I've been trying to nail this for a
First of all, sorry for the vague title. Let me explain. At work we're
Sorry for the vague title, but I'm not exactly sure how I can explain
Sorry for the vague title, I didnt know how to explain it better. I
Sorry for the vague question title, but I'm having a hard time trying to
sorry the issue title is kinda vague. I think i can better explain it
I'm sorry for a pretty vague title, didn't want to turn it into a

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.