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

Provider for Microsoft Excel version 2.0
How to modify data grid view columns with data table bindings
How to make a “Read only” file?
Insert statement when having GUID as primary key
Linq to SQL using global datacontext to keep track of user changes?
Problems in executing AT commands at modem startup
How to Call WebReqeuest.Create on a Webservice
MVC3 how to use #<text></text> as html helper parameter
Matching multiple domains in an XPath expression
Copy object or new object with copied properties?
How do I restrict access to some methods in WCF?
c# Reading event log of remote server
Slow While loop in C#
foreach and linq query - need help trying to understand please
Make a TextBlock in a ScrollViewer Automatically Zoom to Fit
c# - why this OrderBy parameter is wrong?

Categories

HOME
logging
repository
postgresql
oop
model-view-controller
ldap
popover
c++builder
gaussian
hex
magento-1.9.2.1
where
coordinates
snmp
ebay
browser-cache
psexec
gnuradio
zabbix
sahi
coding-style
virtualenv
tracking
sha1
owncloud
css-float
samba
udeploy
typedef
angular2-pipe
matlab-gui
screen-scraping
functional-dependencies
waterfall
datanucleus
folder
multilanguage
wit-ai
multi-user
console.readline
stdin
cowboy
observer-pattern
google-chrome-console
tapply
android-doze-and-standby
rivets.js
seyren
liferay-ide
multi-targeting
jtextarea
critical-section
node.js-client
codesniffer
vitamio
queuing
window.location
pyshark
alchemy
firefox-os
database-project
jts
updating
bootstrap-tabs
dpkt
waterline
veeam
accpac
method-overloading
fedena
mind-manager
selenium-grid2
maven-javadoc-plugin
google-voice
infomaker
zend-db-table
nsnumber
wordbreaker
qmainwindow
pudb
gmail-imap
generator-expression
reference-manual
ntdll
boost.build
freeimage
openafs
filedialog
systrace
code-organization
opendir
hwnd
cpack
qprocess
calendarextender

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