15661

MVC ViewModel - Object Reference Not Set to Instance of an Object

Question:

I receive an "Object reference not set to instance of an object" error while trying to convert a DataTable into a viewmodel containing rows and columns.

<h2>ViewModel</h2> public class ViewModel { public List<ColumnViewModel> Columns { get; set; } public List<RowViewModel> Rows { get; set; } } public class ColumnViewModel { public string Name { get; set; } } public class RowViewModel { public List<CellValueViewModel> Values { get; set; } } public class CellValueViewModel { public string Value { get; set; } } <h2>Model</h2> ViewModel myViewModel = new ViewModel(); CellValueViewModel myCellValueViewModel = new CellValueViewModel(); RowViewModel myRowViewModel = new RowViewModel(); foreach (DataColumn column in GridData.Columns) { ColumnViewModel myColumnViewModel = new ColumnViewModel(); myColumnViewModel.Name = column.ColumnName; myViewModel.Columns.Add(myColumnViewModel); }

The error occurs at myViewModel.Columns.Add(myColumnViewModel); I do not understand why this is happening since Ive instantiated each object that Im using here.

Answer1:

Well, you never assigned the Columns property a value. Make sure you have instantiated it before attempting to add values to this list:

ViewModel myViewModel = new ViewModel(); myViewModel.Columns = new List<ColumnViewModel>();

Recommend

  • Can you help with this MVC ViewModel issue?
  • Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collectio
  • XAML > ViewModel Command parameter - Cannot convert from object to bool?
  • Multiple Defered WHERE clause expressions in LINQ to SQL
  • hamburger menu prism xamarin forms?
  • _Layout.cshtml cannot be requested directly because it calls the “RenderBody” method
  • Cannot convert value of type 'X' to expected argument type 'X'
  • How can I let users share their location in Bot Framework webchat channel?
  • Creating a DropDownList
  • Set the selected item in dropdownlist in MVC3
  • How to create a file in java without a extension
  • Spring Data JPA custom method causing PropertyReferenceException
  • Pass value from viewmodel to script in zk
  • Splitting given String into two variables - php
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • Web-crawler for facebook in python
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Transpose CSV data with awk (pivot transformation)
  • Cannot Parse HTML Data Using Android / JSOUP
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • Turn off referential integrity in Derby? is it possible?
  • apache spark aggregate function using min value
  • Add sale price programmatically to product variations
  • unknown Exception android
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How do I use LINQ to get all the Items that have a particular SubItem?