Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an error

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
com.paypal.sdk.services.CallerServices.set_APIProfile(IAPIProfile value) +49
ASPDotNetSamples.PayPalAPI..ctor() +98
ASPDotNetSamples.wppro.DirectPaymentAuthorization.PayButton_Click(Object sender, EventArgs e) +62
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563



XML
<%@ Page language="c#" Codebehind="DirectPaymentAuthorization.aspx.cs" AutoEventWireup="false" Inherits="ASPDotNetSamples.wppro.DirectPaymentAuthorization" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
    <head>
        <%--<title>TransactionDetails</title>
        <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1" />
        <meta name="CODE_LANGUAGE" content="C#" />
        <meta name="vs_defaultClientScript" content="JavaScript" />
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5" />
        <link href="../sdk.css" rel="stylesheet" type="text/css" />--%>
        <style type="text/css">
            .style1 {
                width: 47px;
            }
        </style>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h1>Direct Payment</h1>
            NOTE: The only currency supported by the Direct Payment API at this time is US
            dollars (USD).<br />
            <br />
            <strong>Payment</strong>
            <table>
                <tr>
                    <td>Amount:
                    </td>
                    <td>
                        <asp:TextBox Runat="server" ID="PaymentAmountTextBox">1.00</asp:TextBox>
                    </td>
                    <td>USD</td>
                </tr>
            </table>
            <br />
            <strong>Buyer Name</strong>
            <table>
                <tbody>
                    <tr>
                        <td>First Name:
                        </td>
                        <td>
                            <asp:TextBox Runat="server" ID="BuyerFirstNameTextBox">John</asp:TextBox>
                        </td>
                        <td><font size="-1" color="red">Required</font></td>
                    </tr>
                    <tr>
                        <td>Last Name:
                        </td>
                        <td>
                            <asp:TextBox Runat="server" ID="BuyerLastNameTextBox">Doe</asp:TextBox>
                        </td>
                        <td><font size="-1" color="red">Required</font></td>
                    </tr>
                </tbody>
            </table>
            <br />
            <strong>Address</strong>
            <table>
                <tr>
                    <td>Address1:
                    </td>
                    <td>
                        <asp:TextBox Runat="server" ID="BuyerAddress1TextBox">123 Fake St</asp:TextBox>
                    </td>
                    <td><font size="-1" color="red">Required</font></td>
                </tr>
                <tr>
                    <td>Address2:
                    </td>
                    <td><asp:TextBox Runat="server" ID="BuyerAddress2TextBox"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>City:
                    </td>
                    <td><asp:TextBox Runat="server" ID="BuyerCityTextBox">Omaha</asp:TextBox></td>
                    <td><font size="-1" color="red">Required</font></td>
                </tr>
                <tr>
                    <td>State:
                    </td>
                    <td><asp:TextBox Runat="server" ID="BuyerStateTextBox">NE</asp:TextBox>
                    </td>
                    <td><font size="-1" color="red">Required</font></td>
                </tr>
                <tr>
                    <td>Zip Code:
                    </td>
                    <td><asp:TextBox Runat="server" ID="BuyerZipCodeTextBox">68104</asp:TextBox></td>
                    <td><font size="-1" color="red">Required</font></td>
                </tr>
                <tr>
                    <td>Country:
                    </td>
                    <td>United States</td>
                </tr>
            </table>
            <br />
            <strong>Credit Card</strong>
            <table>
                <tbody>
                    <tr>
                        <td>Type:
                        </td>
                        <td>
                            <asp:DropDownList Runat="server" ID="CreditCartTypeDropDownList" OnSelectedIndexChanged="CreditCartTypeDropDownList_SelectedIndexChanged"
                                AutoPostBack="True">
                                <asp:ListItem Selected="True" value="Visa">Visa</asp:ListItem>
                                <asp:ListItem value="MasterCard">MasterCard</asp:ListItem>
                                <asp:ListItem value="Discover">Discover</asp:ListItem>
                                <asp:ListItem value="Amex">American Express</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                        <td class="style1"><font size="-1" color="red">Required</font></td>
                    </tr>
                    <tr>
                        <td>Credit Card Number:
                        </td>
                        <td><asp:TextBox runat="server" ID="CreditCardNumberTextBox">4059042064101342</asp:TextBox></td>
                        <td class="style1"><font size="-1" color="red">Required</font></td>
                    </tr>
                    <tr>
                        <td>Card Verification Number:
                        </td>
                        <td><asp:TextBox runat="server" ID="CVV2TextBox" Text='000'>962</asp:TextBox></td>
                        <td class="style1"><font size="-1" color="red">Required</font></td>
                    </tr>
                    <tr>
                        <td>Expiration Date:
                        </td>
                        <td>
                            <asp:DropDownList Runat="server" ID="ExpMonthDropDownList">
                                <asp:ListItem value="01">01</asp:ListItem>
                                <asp:ListItem value="02">02</asp:ListItem>
                                <asp:ListItem value="03">03</asp:ListItem>
                                <asp:ListItem value="04">04</asp:ListItem>
                                <asp:ListItem value="05">05</asp:ListItem>
                                <asp:ListItem value="06">06</asp:ListItem>
                                <asp:ListItem value="07">07</asp:ListItem>
                                <asp:ListItem value="08">08</asp:ListItem>
                                <asp:ListItem value="09">09</asp:ListItem>
                                <asp:ListItem value="10">10</asp:ListItem>
                                <asp:ListItem value="11">11</asp:ListItem>
                                <asp:ListItem value="12">12</asp:ListItem>
                            </asp:DropDownList>
                            <asp:DropDownList Runat="server" ID="ExpYearDropDownList">
                                <asp:ListItem value="2005">2005</asp:ListItem>
                                <asp:ListItem Value="2006">2006</asp:ListItem>
                                <asp:ListItem value="2007">2007</asp:ListItem>
                                <asp:ListItem value="2008">2008</asp:ListItem>
                                <asp:ListItem value="2009">2009</asp:ListItem>
                                <asp:ListItem value="2010">2010</asp:ListItem>
                                <asp:ListItem value="2011">2011</asp:ListItem>
                                <asp:ListItem value="2012">2012</asp:ListItem>
                                <asp:ListItem value="2013">2013</asp:ListItem>
                                <asp:ListItem value="2014">2014</asp:ListItem>
                                <asp:ListItem Selected="True" value="2007">2007</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                        <td class="style1"><font size="-1" color="red">Required</font></td>
                    </tr>

                </tbody>
            </table>
            <br />
            <asp:Button runat="server" ID="PayButton" OnClick="PayButton_Click" Text="Pay" OnClientClick="PayButton_Click"></asp:Button>
             <%--<asp:Button runat="server" ID="PayButton" Text="Pay" />--%>
            <input type="button" value="Cancel" onclick="javascript:history.back()" />
            <h3><a href="../Calls.aspx">Home</a></h3>
        </form>
    </body>
</html>










That was my C# Code


C#
using System;
using System.Web.UI.WebControls;
using com.paypal.soap.api;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Xml;
using System.Web.Configuration;
using ASPDotNetSamples.wppro;

namespace ASPDotNetSamples.wppro
{
	/// <summary>
	/// Summary description for TransactionDetails.
	/// </summary>
	public class DirectPaymentAuthorization : System.Web.UI.Page
	{
		protected Button GetButton;
		protected TextBox PaymentAmountTextBox;
		protected TextBox BuyerFirstNameTextBox;
		protected TextBox BuyerLastNameTextBox;
		protected TextBox BuyerAddress1TextBox;
		protected TextBox BuyerAddress2TextBox;
		protected TextBox BuyerCityTextBox;
		protected TextBox BuyerStateTextBox;
		protected TextBox BuyerZipCodeTextBox;
		protected DropDownList CreditCartTypeDropDownList;
		protected TextBox CreditCardNumberTextBox;
		protected TextBox CVV2TextBox;
		protected DropDownList ExpMonthDropDownList;
		protected DropDownList ExpYearDropDownList;
		protected Button PayButton;
	
		private void Page_Load(object sender, EventArgs e)
		{
            
			if (!this.IsPostBack)
			{
				FillInGeneratedCreditCardNumber();
			}
		}
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);
			this.DataBind();
		}
		public override void DataBind()
		{
			if (this.TransactionID != null)
			{
				PayPalAPI api = new PayPalAPI();
				this.PayPalResponse = api.GetTransactionDetails(this.TransactionID);
			}
			base.DataBind();
		}
		protected void CreditCartTypeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
		{
			FillInGeneratedCreditCardNumber();
		}
		private void FillInGeneratedCreditCardNumber()
		{
			this.CreditCardNumberTextBox.Text = Util.GenerateCreditCardNumber(this.CreditCartTypeDropDownList.SelectedValue);
		}
		protected void PayButton_Click(object sender, EventArgs e)
		{
            string value1 = ConfigurationManager.AppSettings["API_USERNAME"];
            string value2 = ConfigurationManager.AppSettings["API_PASSWORD"];
            string value3 = ConfigurationManager.AppSettings["API_SIGNATURE"];
            string value4 = ConfigurationManager.AppSettings["API_ENVIORMENT"];
			PayPalAPI api = new PayPalAPI();
			int expMonth = int.Parse(this.ExpMonthDropDownList.SelectedValue);
			int expYear = int.Parse(this.ExpYearDropDownList.SelectedValue);
			this.PayPalResponse = api.DoDirectPayment(
				this.PaymentAmountTextBox.Text,
				this.BuyerFirstNameTextBox.Text,
				this.BuyerFirstNameTextBox.Text,
				this.BuyerAddress1TextBox.Text,
				this.BuyerAddress2TextBox.Text,
				this.BuyerCityTextBox.Text,
				this.BuyerStateTextBox.Text,
				this.BuyerZipCodeTextBox.Text,
				this.CreditCartTypeDropDownList.SelectedValue,
				this.CreditCardNumberTextBox.Text,
				this.CVV2TextBox.Text,
				expMonth,
				expYear, PaymentActionCodeType.Authorization);
			Response.Redirect("Receipt.aspx");
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.BuyerFirstNameTextBox.TextChanged += new System.EventHandler(this.BuyerFirstNameTextBox_TextChanged);
			this.ExpYearDropDownList.SelectedIndexChanged += new System.EventHandler(this.ExpYearDropDownList_SelectedIndexChanged);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
		private void BuyerFirstNameTextBox_TextChanged(object sender, System.EventArgs e)
		{
		
		}
		private void ExpYearDropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}
Posted
Updated 5-Jul-11 2:36am
v2

1 solution

This error occurs when you are trying to perform an operation on an attribute that is null.
The best way to find out which object this could be, is to debug your source code line by line, check where the error occurs and then figure out the exact object.
 
Share this answer
 
Comments
khw.hammad 5-Jul-11 8:36am    
The problem is that,controls on aspx page gives an error in design view of aspx page,
Error is:
Error Creating Control - PayButtonObject reference not set to an instance of an object.

so i can not debug the button,where it goes.
kindly recomend me a solution plzzzz
Sergey Alexandrovich Kryukov 5-Jul-11 14:11pm    
Abhinav, I voted 4, but if you remove incorrect word "attribute"...
This is the null instance of the reference object, when you try to call it's method/property.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900