0

I am trying to store a user into my Azure Table but when trying to create a user I'm getting "object not set to an instance of an object", why???

UserEntity entry = new UserEntity();
entry.UserName = pUserName;
entry.MiniatureImageURL = blob.Uri.ToString();
entry.PhotosUrl.Add(blob.Uri.ToString()); //THIS IS A LIST of strings
Connection cn = new Connection();
cn.AddUserEntries(entry);

My connection class is defined as follows:

Here I'm trying to add the new user to the current context and then try to save the item in the storage:

public void AddUserEntries(UserEntity newItem)
{
   try
   {
      this.context.AddObject("UserEntity", newItem);
      this.context.SaveChanges();
   }

   catch (Exception ex)
   {
      throw new Exception(ex.Message);
   }
}

static Connection()
{
   try
   {
      storageAccount = CloudStorageAccount.FromConfigurationSetting("dataconnectionstring");

      CloudTableClient.CreateTablesFromModel(
      typeof(Connection),
         storageAccount.TableEndpoint.AbsoluteUri,
         storageAccount.Credentials);
   }

   catch (Exception ex)
   {
      throw new Exception(ex.Message);
   }
}

public Connection()
{
   try
   {
      this.context = new UserDataContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
      this.context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));
   }

   catch (Exception ex)
   {
      throw new Exception("There was a problem trying to create the user. " + ex.Message);
   }
 }
Brian
  • 5,064
  • 7
  • 33
  • 44
  • 2
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 03 '13 at 18:44
  • Why are you using `catch(Exception ex){throw new Exception(ex.Message);}` You'd be better off not catching exceptions at all. – John Saunders Apr 03 '13 at 18:45
  • the problem is that I do not know what could be the error since I cannot test this in my local pc since the azure emulator is not working, and when trying on the cloud I dont know what is being null here – Bryan Arbelo - MaG3Stican Apr 03 '13 at 18:47
  • Use your eyes. You should be able to see where you're deferencing an object that might be null. – John Saunders Apr 03 '13 at 18:48

2 Answers2

3

The only object you are dereferencing in the code is blob.Uri. Check if blob is null, or if blob.Uri is null.

pascalhein
  • 5,372
  • 4
  • 29
  • 41
2

If you use

try
{
    ...
}
catch (Exception ex)
{
    throw new Exception(ex.Message);
}

You lose your stack trace. If you remove these try catch blocks. You should be able to get a correct stack trace for the line that caused the problem. You can put a breakpoint in your code on that line to see what part of it is null. At that point you can look to see WHY that value is null, as well as adding proper safe guards to insure your code will not excute that line when that value is null.

Nick Freeman
  • 1,371
  • 1
  • 10
  • 24
  • thanks, did not know, I got used by my teachers to try catch everything.. – Bryan Arbelo - MaG3Stican Apr 03 '13 at 19:06
  • No problem. Here is [a good article on which exceptions to catch](http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx) if you are looking for more information. You should never catch a general exception outside of the root of your application because this could also catch exceptions that you do not want to handle like `OutOfMemoryException` and `ThreadAbortException`. – Nick Freeman Apr 03 '13 at 19:12
  • another thing to note is that if you are catching a more specific type at a higher level, like `InvalidOperationException` it will not go into the correct catch block because the new type of the exception is only `Exception`. If you are looking to rethrow the exception after doing some handling you would use `throw ex` instead of `throw new Exception(ex.Message)` – Nick Freeman Apr 03 '13 at 19:18
  • 1
    It is even better to just `throw;` instead of `throw ex;` http://stackoverflow.com/questions/730250/is-there-a-difference-between-throw-and-throw-ex – pascalhein Apr 03 '13 at 19:50
  • @csharpler that is correct – Nick Freeman Apr 03 '13 at 20:14
  • Even better in this case is to not catch the exceptions at all! Only catch exceptions you can actually _handle_. In this case, you're not handling it (nothing gets fixed), so don't catch it at all. – John Saunders Apr 03 '13 at 21:09
  • 1
    @MaG3Stican: if your teachers are telling you to catch everything, tell them I said they should stop teaching you worst practices. – John Saunders Apr 03 '13 at 21:11
  • 1
    Couldn't agree more with John on this. Don't do something because someone told you to. Do it because it makes sense. – NotMe Apr 03 '13 at 23:59