Note that there are some explanatory texts on larger screens.

plurals
  1. PONullReferenceException on ForEach JSON c#
    primarykey
    data
    text
    <p>I am having a problem with the following code. It uses the Facebook c# SDK. The problem is that I am pulling and printing a list of all of my friends work history. However, when this data isn't available I think it is causing a NullReferenceException. I have looked at a bunch of solutions online, but haven't found one that seems to work. Any help would be appreciated.</p> <pre><code>public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var accessToken = "..."; var client = new FacebookClient(accessToken); dynamic myInfo = client.Get("me/friends", new { fields = "name,id,work" }); foreach (dynamic friend in myInfo) { foreach (dynamic work in myInfo.work) // &lt;---- here { Response.Write("Employer: " + work.employer.name + "&lt;br/&gt; Position:" + work.position.name + "&lt;br/&gt;&lt;br/&gt;"); } } } } </code></pre> <p>I have included the suggested edits but am now getting a different error: </p> <pre><code> namespace WebApplication1.Site { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var accessToken = "xxx"; var client = new FacebookClient(accessToken); dynamic myInfo = client.Get("me/friends", new { fields = "name,id,work" }); foreach (dynamic friend in myInfo) { **foreach (dynamic work in friend.work ?? new[] { new { employer = new { name = string.Empty }, position = new { name = string.Empty } } })** { Response.Write("Employer: " + myInfo.work.employer.name); } } } } } </code></pre> <p>'System.Collections.Generic.KeyValuePair' does not contain a definition for 'work' on Line 21</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload