Zardify
Zardify

Reputation: 23

PayPal .NET SDK - new Payment() - throws NullReference exception

I'm trying to implement PayPal through their .NET SDK. (C#) I'm working with ASP.NET (Web Forms). Whenever I try to create (simply by constructor) a new Payment object, I get a NullReferenceException with absolutely NO detail in it...

Here's the exception:

System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=<SolutionName>
StackTrace:
   at <SolutionName>.Classes.MFPal.Checkout(List`1 cartItems) in C:\<SolutionName>\<FolderName>\Classes\MFPal.cs:line 46
   at <SolutionName>.Pages.Private.ShoppingCart.btnPPCheckout_Click(Object sender, EventArgs e) in C:\<SolutionName>\<FolderName>\Pages\Private\ShoppingCart.aspx.cs:line 164
   at System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e)
   at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: 

InnerException is null by the way.

I've already decoupled the code to the bare minimum, I've tested first with a filled ItemList, now I'm just using an empty one. Now I'm also using "0" for every price, but my first tests were made with correct numbers. At first I used simply;

Payment.Create(apiContext, new Payment() { ... });

But I couldn't identify the NullReference, so now the code looks like this:

        var apiContext = new APIContext(Config.PayPalAccessToken);
        apiContext.Config = ConfigManager.Instance.GetProperties();
        // ONLY ADDED FOR MORE TESTING.
        apiContext.HTTPHeaders = new Dictionary<string, string>();

        // NULLREF ON THE NEXT LINE
        Payment pym = new Payment() {
            intent = "sale",
            redirect_urls = {
                return_url = "http://<DomainName>/home",
                cancel_url = "http://<DomainName>/home"
            },
            payer = { payment_method = "paypal" },
            transactions = new List<Transaction>() {
                new Transaction() {
                    amount = {
                        currency = "USD",
                        total = "0",
                        details = new Details() {
                            tax = "0",
                            shipping = "0",
                            subtotal = "0"
                        }
                    },
                    item_list = new ItemList() { items = new List<Item>() },
                    description = "asd",
                    invoice_number = "1111111"
                }
            }
        };

Is anyone experiencing similar problems? Am I missing something? A needed property maybe? When I debug, I can't find anything null.

Thank you in advance!

Upvotes: 0

Views: 291

Answers (1)

Zardify
Zardify

Reputation: 23

I've found the problem. Went through it line by line until I realized it. I forgot (and Visual Studio liked the idea) to leave out:

new Amount() {...}

Constructor from the line:

amount = {...}

I'm sorry for the seemingly pointless post. Maybe someone will eventually have the same mistake.

Upvotes: 1

Related Questions