title: Object reference not set to an instance of an object amazon s3
body: <p>Here is a simple class I have created:</p>
  
  <pre><code>public delegate void ChangeEventHandler(object sender, UploadEventArgs e);
  
  public class UploadEventArgs : EventArgs
  {
      public string FileName;
      public string OldFileName;
      //public string ThumbnailName;
      public string ContentType;
      public int ContentLength;
  }
  
  public class Bucket
  {
  
      #region Events
  
      public event ChangeEventHandler OnStart;
      public event ChangeEventHandler OnComplete;
      public event ChangeEventHandler OnError;
  
      #endregion
  
      #region Fields
  
      private string _key;
      private string _secret;
      private const string _seviceUrl = "s3-eu-west-1.amazonaws.com";
  
      #endregion
  
      #region Properties
  
      public string BucketName { get; set; }
  
      #endregion
  
      #region Constructors
  
      public Bucket(string key, string secret)
      {
          this._key = key;
          this._secret = secret;
      }
  
      public Bucket(string key, string secret, string bucketName)
      {
          this._key = key;
          this._secret = secret;
          this.BucketName = bucketName;
      }
  
      #endregion
  
      #region Public methods
  
      /// &lt;summary&gt;
      /// Creates a bucket in the default EUWest1 region endpoint
      /// &lt;/summary&gt;
      /// &lt;param name="bucketName"&gt;The name of the new bucket&lt;/param&gt;
      public void Create(string bucketName)
      {
          Create(bucketName, RegionEndpoint.EUWest1);
      }
  
      /// &lt;summary&gt;
      /// Creates a bucket in the specified region endpoint
      /// &lt;/summary&gt;
      /// &lt;param name="bucketName"&gt;The name of the new bucket&lt;/param&gt;
      /// &lt;param name="region"&gt;The region endpoint of the bucket&lt;/param&gt;
      public void Create(string bucketName, RegionEndpoint region)
      {
          using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(region))
          {
              // Creates the bucket.
              S3DirectoryInfo rootDirectory = new S3DirectoryInfo(client, bucketName);
              rootDirectory.Create();
          }
          this.BucketName = bucketName; // Set our bucketname property
      }
  
      public string UploadMultipartFile(HttpPostedFile file)
      {
          if (string.IsNullOrEmpty(BucketName))
              throw new Exception("The bucketname must be supplied."); // Throw an exception if the bucketname has not been initiated
  
          string keyName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
  
          UploadEventArgs e = new UploadEventArgs()
          {
              FileName = keyName,
              OldFileName = file.FileName,
              ContentType = file.ContentType,
              ContentLength = file.ContentLength
          };
  
          try
          {
              OnStart(this, e);
          }
          catch (Exception ex)
          {
          }
  
          var config = new AmazonS3Config()
          {
              ServiceURL = _seviceUrl
          };
          AmazonS3 client = new AmazonS3Client(_key, _secret, config);
  
          // List to store upload part responses.
          List&lt;UploadPartResponse&gt; uploadResponses = 
              new List&lt;UploadPartResponse&gt;();
  
          // 1. Initialize.
          InitiateMultipartUploadRequest initiateRequest = 
              new InitiateMultipartUploadRequest()
              .WithBucketName(BucketName)
              .WithKey(keyName);
  
          InitiateMultipartUploadResponse initResponse =
              client.InitiateMultipartUpload(initiateRequest);
  
          // 2. Upload Parts.
          long contentLength = file.InputStream.Length;
          long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB
  
          try
          {
              long filePosition = 0;
              for (int i = 1; filePosition &lt; contentLength; i++)
              {
  
                  // Create request to upload a part.
                  var uploadRequest = new UploadPartRequest()
                      .WithBucketName(BucketName)
                      .WithKey(keyName)
                      .WithUploadId(initResponse.UploadId)
                      .WithPartNumber(i)
                      .WithPartSize(partSize)
                      .WithFilePosition(filePosition)
                      .WithInputStream(file.InputStream);
  
                  // Upload part and add response to our list.
                  uploadResponses.Add(client.UploadPart((UploadPartRequest)uploadRequest));
  
                  filePosition += partSize;
              }
  
              // Step 3: complete.
              CompleteMultipartUploadRequest completeRequest = 
                  new CompleteMultipartUploadRequest()
                  .WithBucketName(BucketName)
                  .WithKey(keyName)
                  .WithUploadId(initResponse.UploadId)
                  .WithPartETags(uploadResponses);
  
              CompleteMultipartUploadResponse completeUploadResponse =
                  client.CompleteMultipartUpload(completeRequest);
  
              OnComplete(this, e); 
          }
          catch (Exception exception)
          {
              Console.WriteLine("Exception occurred: {0}", exception.Message);
              client.AbortMultipartUpload(new AbortMultipartUploadRequest()
                  .WithBucketName(BucketName)
                  .WithKey(keyName)
                  .WithUploadId(initResponse.UploadId));
  
              OnError(this, e);
          }
  
          return keyName;
      }
  
      #endregion
  
  }
  </code></pre>
  
  <p>The problem I have , is that when "OnStart" is called I get an error stating "Object reference not set to an instance of an object.". 
  I have changed the delegate to just have "UploadEventArgs" as a parameter and I got the same problem.
  I commented out any properties that were not being used, but I still get the error.</p>
  
  <p>I know it is something really simple, but I can't see the wood for the trees....</p>
  
  <p>Any help would be greatly appreciated...
  /r3plica</p>