我正在尝试将用户存储到我的Azure表中但是在尝试创建用户时我得到“对象未设置为对象的实例”,为什么???
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);
我的连接类定义如下:
这里我尝试将新用户添加到当前上下文,然后尝试将该项目保存在存储中:
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);
}
}
答案 0 :(得分:3)
您在代码中取消引用的唯一对象是blob.Uri
。检查blob
是null
,还是blob.Uri
是null
。
答案 1 :(得分:2)
如果您使用
try
{
...
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
您丢失了堆栈跟踪。如果删除这些try catch块。您应该能够为导致问题的行获取正确的堆栈跟踪。您可以在该行的代码中放置一个断点,以查看它的哪一部分为空。此时,您可以查看为什么该值为null,以及添加适当的安全保护措施,以确保当该值为null时,您的代码不会执行该行。