title: PayPal C# .Net SDK Credit Card payment sample null reference exception
body: <p>Thank you for looking at my question.</p>
  
  <p>I'm trying to make use of the PayPal SDK.
  As soon as:</p>
  
  <pre class="lang-cs prettyprint-override"><code>payment.Create(apiContext)
  </code></pre>
  
  <p>executes, it crashes stating:</p>
  
  <pre class="lang-none prettyprint-override"><code>An exception of type 'PayPal.PayPalException' occurred in PayPal.dll but was not handled in user code
  
  Additional information: Object reference not set to an instance of an object.
  </code></pre>
  
  <p>I realize that this is a standard Null reference exception, I just can't figure out what's wrong because it works in the code sample.
  This code was taken directly from <a href="https://github.com/paypal/PayPal-NET-SDK" rel="nofollow">Paypal's SDK Samples</a>, more specifically this <a href="https://github.com/paypal/PayPal-NET-SDK/blob/develop/Samples/Source/PaymentWithCreditCard.aspx.cs" rel="nofollow">page</a>.</p>
  
  <p>Here is my version of it:</p>
  
  <pre class="lang-cs prettyprint-override"><code>    Dictionary&lt;string, string&gt; _paypalConfig;
      Dictionary&lt;string, string&gt; PaypalConfig
      {
          get
          {
              if (_paypalConfig == null)
              {
                  _paypalConfig = ConfigManager.Instance.GetProperties();
              }
              return _paypalConfig;
          }
      }
  
      String _paypalAccessToken;
      String PaypalAccessToken
      {
          get
          {
              if (_paypalAccessToken == null)
              {
                  _paypalAccessToken = new OAuthTokenCredential(PaypalConfig).GetAccessToken();
              }
              return _paypalAccessToken;
          }
      }
  
      APIContext _paypalApiContext;
      APIContext PaypalApiContext
      {
          get
          {
              if (_paypalApiContext == null)
              {
                  _paypalApiContext = new APIContext(PaypalAccessToken);
              }
              return _paypalApiContext;
          }
      }
  
      public ActionResult Pay()
      {
          var apiContext = PaypalApiContext;
  
          // A transaction defines the contract of a payment - what is the payment for and who is fulfilling it. 
          var transaction = new Transaction()
          {
              amount = new Amount()
              {
                  currency = "USD",
                  total = "7",
                  details = new Details()
                  {
                      shipping = "1",
                      subtotal = "5",
                      tax = "1"
                  }
              },
              description = "This is the payment transaction description.",
              item_list = new ItemList()
              {
                  items = new List&lt;Item&gt;()
                  {
                      new Item()
                      {
                          name = "Item Name",
                          currency = "USD",
                          price = "1",
                          quantity = "5",
                          sku = "sku"
                      }
                  },
                  shipping_address = new ShippingAddress
                  {
                      city = "Johnstown",
                      country_code = "US",
                      line1 = "52 N Main ST",
                      postal_code = "43210",
                      state = "OH",
                      recipient_name = "Joe Buyer"
                  }
              }
          };
  
          // A resource representing a Payer that funds a payment.
          var payer = new Payer()
          {
              payment_method = "credit_card",
              funding_instruments = new List&lt;FundingInstrument&gt;()
              {
                  new FundingInstrument()
                  {
                      credit_card = new CreditCard()
                      {
                          billing_address = new Address()
                          {
                              city = "Johnstown",
                              country_code = "US",
                              line1 = "52 N Main ST",
                              postal_code = "43210",
                              state = "OH"
                          },
                          cvv2 = "874",
                          expire_month = 11,
                          expire_year = 2018,
                          first_name = "Joe",
                          last_name = "Shopper",
                          number = "4877274905927862",
                          type = "visa"
                      }
                  }
              },
              payer_info = new PayerInfo
              {
                  email = "test@email.com"
              }
          };
  
          // A Payment resource; create one using the above types and intent as `sale` or `authorize`
          var payment = new Payment()
          {
              intent = "sale",
              payer = payer,
              transactions = new List&lt;Transaction&gt;() { transaction }
          };
  
          // Create a payment using a valid APIContext
          var createdPayment = payment.Create(apiContext);
  
          return View();
      }
  </code></pre>
  
  <p>The full exception is as follows:</p>
  
  <pre class="lang-none prettyprint-override"><code>PayPal.PayPalException was unhandled by user code
    HResult=-2146233088
    Message=Object reference not set to an instance of an object.
    Source=PayPal
    StackTrace:
     at PayPal.PayPalResource.ConfigureAndExecute[T](Dictionary`2 config, IAPICallPreHandler apiCallPreHandler, HttpMethod httpMethod, String resourcePath)
     at PayPal.PayPalResource.ConfigureAndExecute[T](APIContext apiContext, HttpMethod httpMethod, String resource, String payload)
     at PayPal.Api.Payment.Create(APIContext apiContext, Payment payment)
     at PayPal.Api.Payment.Create(APIContext apiContext)
     at PaypalTest.Controllers.HomeController.Pay() in c:\Users\Rian\Documents\Visual Studio 2013\Projects\PaypalTest\PaypalTest\Controllers\HomeController.cs:line 138
     at lambda_method(Closure , ControllerBase , Object[] )
     at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
     at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;BeginInvokeSynchronousActionMethod&gt;b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
     at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3d()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;&gt;c__DisplayClass46.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3f()
    InnerException: System.NullReferenceException
     HResult=-2147467261
     Message=Object reference not set to an instance of an object.
     Source=PayPal
     StackTrace:
          at PayPal.Api.RESTAPICallPreHandler.GetProcessedEndPoint()
          at PayPal.Api.RESTAPICallPreHandler.GetEndpoint()
          at PayPal.PayPalResource.ConfigureAndExecute[T](Dictionary`2 config, IAPICallPreHandler apiCallPreHandler, HttpMethod httpMethod, String resourcePath)
     InnerException: 
  </code></pre>
  
  <p>The relevant web.config sections are as follows:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;configuration&gt;
  &lt;configSections&gt;
      &lt;section name="paypal" type="PayPal.SDKConfigHandler, PayPal" /&gt;
  &lt;/configSections&gt;
  &lt;configuration&gt;
  
  &lt;paypal&gt;
  &lt;settings&gt;
    &lt;add name="mode" value="sandbox"/&gt;
    &lt;add name="connectionTimeout" value="360000"/&gt;
    &lt;add name="requestRetries" value="1"/&gt;
    &lt;add name="clientId" value="AUASNhD7YM7dc5Wmc5YE9pEsC0o4eVOyYWO9ezXWBu2XTc63d3Au_s9c-v-U"/&gt;
    &lt;add name="clientSecret" value="EBq0TRAE-4R9kgCDKzVh09sm1TeNcuY-xJirid7LNtheUh5t5vlOhR0XSHt3"/&gt;
  &lt;/settings&gt;
  &lt;/paypal&gt;
  </code></pre>