c#


C# Add Object To List


I have created a new empty List of type User (User is an Entity in my EDM)
List<User> AvailableLocums = new List<User>();
AvailableLocums = null;
I also have another List of type User which is populated with the results of a query
List<User> Locums = _shiftDateService.GetAvailableLocums(id, shiftDate.shiftDateID).ToList();
I then wish to loop around the Locums List, do some logic, and then add the User to my AvailableLocums List
foreach (var locum in Locums)
{
//Do some logic
AvailableLocums.Add(locum);
}
However, when I try to do this I get the following error
Object reference not set to an instance of an object.
I then tried to amend my code and do the following
foreach (var locum in Locums)
{
//Do some logic
User locumUser = new User();
locumUser = locum;
AvailableLocums.Add(locumUser);
}
But again I get the same error
Object reference not set to an instance of an object.
Could someone please help me with this?
Thanks.
You are nulling out that list
List<User> AvailableLocums = new List<User>();
AvailableLocums = null; // HERE
hence, the exception. You don't need this line to make an empty list:
AvailableLocums = null;
You are telling your variable "AvailableLocums" to no longer point to anything in memory.
Remove:
AvailableLocums = null;
how about:
List<User> AvailableLocums;
List<User> Locums;
// get id for shift date service
// int id = ...
// get locums from shift date service
Locums = _shiftDateService.GetAvailableLocums(id, shiftDate.shiftDateID).ToList();
// time to add to AvailableLocums
if(AvailableLocums == null)
AvailableLocums = new List<User>();
foreach (var locum in Locums)
{
//Do some logic
AvailableLocums.Add(locum);
}
get out this line :
AvailableLocums = null;
and it will be Ok ...
You declaring new object and on next row set it to null?
try to
List<User> Locums = new List<User>();
before _shiftDateService.GetAvailableLocums...
Try this code.
List<User> AvailableLocums = null;
List<User> Locums = _shiftDateService.GetAvailableLocums(id, shiftDate.shiftDateID).ToList();
AvailableLocums = Locums.Where(newUserRecord => newUserRecord != null).ToList();
* you can take any name instead of "newUserRecord".

Related Links

How to scroll down while page loading - GeckoFx C#
How to convert a 2D binary object in to a Dictonary<string, object> in C# using Binary Formatter
From where would a referenced assembly's culture info be loaded? And how to set it to “neutral”?
How to make a Form load before opening another?
Unity .dae object not rotating on touch with c# script
Pass text of Listview.ItemTemplate to antoher frame uwp
WPF Prism initializing tabs with ICollection using Navigation
AJAX call returns 'undefined'
C# Linking windows form with already created classes and console
C# responsible form on excel
How to perform ctrl+p for print in c# selenium?
System.Data.Entity.Infrastructure.DbUpdateException error even when i am not updating the id column
C# get cmd output just as shown in real cmd window
Clicked 2 Button At Once
how to add a server provider name on visual studio 2013 Mvc4 c#
HttpResponse - how to set status code depends on PushStreamContent execution result

Categories

HOME
autohotkey
alasql
postsharp
sdk
confluence-rest-api
sugarcrm
richtextbox
psexec
gnuradio
pdf-generation
install
qt4
genexus
soap-client
phpexcel
google-search-appliance
javascript-debugger
jinja2
multibinding
beacon
draw2d
spring-webflow
xna
devexpress-mvc
windowserror
datanucleus
inspec
mongoose-im
arp
visual-studio-debugging
microstrategy
openvms
dropdownbox
pymunk
respect-validation
ssdp
mtm
superclass
intervention
cakephp-3.2
inner-join
html-entities
text-formatting
django-tables2
noise
monaca
beanshell
fluent-nhibernate-mapping
sp
kendo-scheduler
hellosign
cc
vertex-shader
blitz3d
timefield
access-violation
gdi
php-java-bridge
boost-bind
laterjs
python-rq
chomsky-normal-form
encode
recode
webservices-client
serial-communication
transbase
traminer
winforms-interop
transparentproxy
grails-3.0.9
opennebula
beagleboard
xmla
touch-typing
method-overloading
os.walk
nsnotificationcenter
indic
disabled-control
angular-file-upload
nodeunit
connections
starling-framework
.net-reflector
google-oauth-java-client
fade
infomaker
jython-2.5
leap-year
database-restore
metapost
quit
cryptarithmetic-puzzle
tasklist
waveout
pdf-reader
motorola-droid
idtabs
scriptmanager
apache-commons-email
httppostedfilebase
lightopenid
netduino
jquery-attributes
mdd
manuals
script-tag
djangoappengine
user-preferences
expander
ironpython-studio

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App