接收错误对象引用未设置为对象的实例? [英] Receiving error Object reference not set to an instance of an object?

查看:145
本文介绍了接收错误对象引用未设置为对象的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在这里定义了一个类卖方我使用的是公共字符串变量

  public   string  _connectionString = ConfigurationManager.ConnectionStrings [ 卖方]。ConnectionString.ToString(); 



上面的连接字符串在App.config中定义类库卖家使用的文件。



当我打电话给卖家时

卖家_seller =  new 卖家(); 



抛出错误

对象引用没有设置为对象的实例当光标读取上面的字符串对象?? 



它非常恼人的错误。我尝试了所有的东西,但没有找到任何解决方案。

请帮忙。



谢谢

解决方案

首先从调试器开始,看看究竟发生了什么。

最初,将该行更改为声明:

< pre lang =c#> public string _connectionString = null ;



并在您的Seller类构造函数中添加以下内容:

  var  o1 = ConfigurationManager.ConnectionStrings [ 卖方]; 
var o2 = o1.ConnectionString;
_connectionString = o2.ToString();

然后在第一行放置一个断点并逐步执行,使用调试器查看值 - 当你知道哪一个为null时,你可以开始看看为什么 - 但直到你知道,你只是在猜测。



我们不能为你做这些 - 我们无法访问你的机器或web.config文件。


Hi
I have defined a class "Seller" in this I am using a public string variable as

public string _connectionString = ConfigurationManager.ConnectionStrings["seller"].ConnectionString.ToString();


The above connection string is defined in App.config file used in class library Seller.

When I am calling class Seller

Seller _seller = new Seller();


It throws error

" Object reference not set to an instance of an object" when the cursor reads the above string object??


Its very irritating Error. I tried every thing but didn,t found any solution.
Please help on this.

Thanks

解决方案

So start with the debugger and look at exactly what is going on.
Initially, change that line to just the declaration:

public string _connectionString = null;


and in your Seller class constructor add this:

var o1 = ConfigurationManager.ConnectionStrings["seller"];
var o2 = o1.ConnectionString;
_connectionString = o2.ToString();

Then put a breakpoint on the first line and step through, using the debugger to look at the values - when you know which one is null, you can start looking at why - but until you know that, you are just guessing.

We can't do this for you - we don't have access to your machine or the web.config file.


这篇关于接收错误对象引用未设置为对象的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆