title: "Object reference not set to an instance of an object" while sending JSON to a webservice using JQuery/AJAX
body: <p>EDIT: After figuring out how to step through the code it appears that <code>An exception of type 'System.NullReferenceException' occurred in DataManagement.dll but was not handled in user code</code>. It happens in this line:</p>
  
  <pre><code>string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");
  </code></pre>
  
  <p>I'm not sure what the means but hopefully that sheds some more light on the problem.</p>
  
  <p>I am trying to send a JavaScript object to a web Service and I am getting the 500 error: "Object reference not set to an instance of an object". </p>
  
  <p>I'm fairly positive that I am passing something out of format to the method in the web service but I'm not really sure. Any help I can get would be awesome I'm a beginner and have been ramming my head against the wall on this for forever.</p>
  
  <p>JSON (from fiddler):</p>
  
  <pre><code>{"sampleIds":["1"],"line1":"","line2":"","line3":"","labelType":"Soil","startingLabelPosition":1}
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>var labelInfo = new Object();
  labelInfo.sampleIds = sampleIds;
  labelInfo.line1 = $('#ddlGrower').val();
  labelInfo.line2 = $('#ddlFarm').val();
  labelInfo.line3 = $('#ddlField1').val();
  labelInfo.labelType = $('#ddlSoilLoginMatrix option:selected').text();
  labelInfo.startingLabelPosition = parseInt($('#labelSelection').text());
  
  $.ajax({
      type: "POST",
      url: "DesktopModules/DataManagement/TestService.svc/CreateLabelPdf",
      data: JSON.stringify(labelInfo),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(data) {
          window.open(data.d);
      },
      error:  function(msg) {
          alert("Error: " + msg.status);
      }
  });
  </code></pre>
  
  <p>C# web service:</p>
  
  <pre><code>[OperationContract]
  public string CreateLabelPdf(List&lt;string&gt; sampleIds, string line1, string line2, string line3, string labelType, int startingLabelPosition)
  {
     List&lt;LabelContent&gt; labels = new List&lt;LabelContent&gt;();
  
     foreach (var sample in sampleIds)
     {
        LabelContent labelContent = new LabelContent();
        labelContent.Line1 = line1;
        labelContent.Line2 = line2;
        labelContent.Line3 = line3;
        labelContent.LabelId = sample;
        labels.Add(labelContent);
     }   
  
     Creator creator = new Creator
     {
        IncludeLabelBorders = false
     };
     string path = HttpContext.Current.Server.MapPath(@"~\DesktopModules\DataManagement\Pdf\" + 0 + ".pdf");
     creator.PrintLabels(labels, new Avery5160(), path, startingLabelPosition);
     return path;
  }
  </code></pre>