获取对象引用未设置为对象的实例。为什么? [英] Getting Object reference not set to an instance of an object. why?

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

问题描述

  var  split1 = str.Split( new  [] { ' 〜'' ,'' !'}); 

for int i = 0 ; i < split1.Count(); i + = 3
{
Label [] LabelName = new 标签[split1.Count()];
LabelName [i] .Name = split1 [i] .ToString();
}





 LabelName.Location =  new  Point(Convert.ToInt32(split1 [i +  1 ]),Convert.ToInt32(split1 [i +  2 ]); 

解决方案

这是最简单的错误。所有你需要做的就是当代码停止异常或命中你设置的断点时检查变量内容。



调试器在那里向你显示你有多少不知道你自己的代码和数据。


这是因为LabelName是一个emtry数组,你应该初始化数组中的每个元素,然后才能访问它的属性。试试这个:



  var  split1 = str.Split( new  [] {' 〜'' ,'' !'}); 

Label [] LabelName =( from sp in split1
选择 new 标签{
Name = sp})。ToArray();


var split1 = str.Split(new[] { '~', ',', '!' });

 for (int i = 0; i < split1.Count(); i += 3)
            {
Label[] LabelName = new Label[split1.Count()];
                   LabelName[i].Name = split1[i].ToString();
}



LabelName.Location = new Point(Convert.ToInt32(split1[i + 1]), Convert.ToInt32(split1[i + 2]));

解决方案

This is the easiest error to figure out. All you have to do is inspect variable contents when the code stops for an exception or hits a breakpoint you set.

The debugger is there to show you just how much you don't know about your own code and data.


It's because LabelName is an emtry array, you should initialize each element in the array before you can access its properties. Try this:

var split1 = str.Split(new[] { '~', ',', '!' });
 
Label[] LabelName = (from sp in split1
                     select new Label{
                     Name = sp}).ToArray();


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

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