Object reference not set to an instance of an objecthttp://forums.asp.net/t/1794258.aspx/1?Object+reference+not+set+to+an+instance+of+an+objectWed, 18 Apr 2012 13:28:40 -040017942584938972http://forums.asp.net/p/1794258/4938972.aspx/1?Object+reference+not+set+to+an+instance+of+an+objectObject reference not set to an instance of an object <p>Ive done this lots of times but this time i am struggling</p> <p></p> <p>the error is at the part <strong>new Lists.lGrid { .......... };</strong></p> <p></p> <pre class="prettyprint">public List&lt;Lists.lGrid&gt; theGrid(List&lt;Lists.lFirms&gt; lfirm, List&lt;Lists.lCRMProgress&gt; lprogress) { var grid = from f in lfirm join p in lprogress on f.id equals p.firmid into l_f from p in l_f.DefaultIfEmpty() select new Lists.lGrid { address1 = f.address1, address2 = f.address2, address3 = f.address3, address4 = f.address4, country = f.country, createddate = f.createddate, email = f.email, faxno = f.faxno, firmid = f.id, id = f.id, firmsize = f.firmsize, firmsizeid = f.firmsizeid, isaccountancy = f.isaccountancy, lastbooking = f.lastbooking, lastevent = f.lastevent, lasteventafterst = f.lasteventafterst, logo = f.logo, modifieddate = f.modifieddate, monthcount = f.monthcount, monthspend = f.monthspend, name = f.name, postcode = f.postcode, programformatid = f.programformatid, quartercount = f.quartercount, quarterspend = f.quarterspend, sector = f.sector, sectorid = f.sectorid, smartplanexpiry = f.smartplanexpiry, spactive = f.spactive, STActive = f.STActive, stexpirydate = f.stexpirydate, telno = f.telno, totalcount = f.totalcount, totalspend = f.totalspend, webplanexpiry = f.webplanexpiry, websiteurl = f.websiteurl, wpactive = f.wpactive, yearcount = f.yearcount, yearspend = f.yearspend, uid = p.uid, crmstatus = p.crmstatus == null ? &quot;New&quot; : p.crmstatus, crmstatusid = p.crmstatusid , crmtype = p.crmtype == null ? &quot;&quot; : p.crmtype, crmtypeid = p.crmtypeid , reviewdate = p.reviewdate == null ? Convert.ToDateTime(&quot;2000-01-01 00:00:00&quot;) : p.reviewdate, updatedate = p.updatedate == null ? Convert.ToDateTime(&quot;2000-01-01 00:00:00&quot;) : p.updatedate, username = p.updatedate == null ? &quot;None&quot; : p.username }; return grid.ToList&lt;Lists.lGrid&gt;(); }</pre> <p><br> <br> </p> 2012-04-18T13:01:12-04:004938977http://forums.asp.net/p/1794258/4938977.aspx/1?Re+Object+reference+not+set+to+an+instance+of+an+objectRe: Object reference not set to an instance of an object <p></p> <blockquote><span class="icon-blockquote"></span> <h4>ArchieEric</h4> <span class="kwd">return</span><span class="pln"> grid</span><span class="pun">.</span><span class="typ">ToList</span><span class="pun">&lt;</span><span class="typ">Lists</span><span class="pun">.</span><span class="pln">lGrid</span><span class="pun">&gt;()</span></blockquote> <p></p> <p>before this check for grid is not null.</p> 2012-04-18T13:03:00-04:004938980http://forums.asp.net/p/1794258/4938980.aspx/1?Re+Object+reference+not+set+to+an+instance+of+an+objectRe: Object reference not set to an instance of an object <p>its not null, lfirm is packed with data</p> <p></p> <p>and it doesnt get that far</p> 2012-04-18T13:04:30-04:004939005http://forums.asp.net/p/1794258/4939005.aspx/1?Re+Object+reference+not+set+to+an+instance+of+an+objectRe: Object reference not set to an instance of an object <p>cause of this error is empty datasource</p> <p></p> 2012-04-18T13:15:00-04:004939021http://forums.asp.net/p/1794258/4939021.aspx/1?Re+Object+reference+not+set+to+an+instance+of+an+objectRe: Object reference not set to an instance of an object <p>Some selected field is null. You should probably add some more NULL checks like the ones for the reviewdate and updatedate fields.</p> 2012-04-18T13:19:55-04:004939043http://forums.asp.net/p/1794258/4939043.aspx/1?Re+Object+reference+not+set+to+an+instance+of+an+objectRe: Object reference not set to an instance of an object <p>you performed a left join, so p can be null, thus any reference to p.??? can fail. your null handling logic is wrong. it shoudl be:</p> <pre class="prettyprint">crmstatus = (p == null || p.crmstatus == null) ? &quot;New&quot; : p.crmstatus,</pre> <pre class="prettyprint"><span class="pun">....</span></pre> 2012-04-18T13:28:40-04:00