C#问题:未处理的“System.NullReferenceException”类型的异常出现在 XXX 其他信息: 未将对象引用设置到对象的实例。
public partial class Form1 : Form
{
private class 图片列表
{
public static int 图像宽度=48;
public static int 图像长度=64;
public Image 项目图像;
public string 字符项目;
public static int 图像宽度属性
{
get
{
return 图像宽度;
}
set
{
图像宽度 = 48;
}
}
public static int 图像长度属性
{
get
{
return 图像长度;
}
set
{
图像长度 = 64;
}
}
public Image 项目图像属性
{
get
{
return 项目图像;
}
set
{
项目图像 = null;
}
}
public string 字符项目属性
{
get
{
return 字符项目;
}
set
{
字符项目 = "";
}
}
}
public Form1()
{
InitializeComponent();
listBox1 .ItemHeight = 图片列表.图像长度属性 + 2;
}
private void Form1_Load(object sender, EventArgs e)
{
图片列表[] 我的图像 = new 图片列表[8];
我的图像[0].项目图像属性 = _138赵志华高手版.Properties.Resources.蔡依林;
我的图像[0].字符项目属性 = "蔡依林";
我的图像[1].项目图像属性 = _138赵志华高手版.Properties.Resources.陈德容;
我的图像[1].字符项目属性 = "陈德容";
我的图像[2].项目图像属性 = _138赵志华高手版.Properties.Resources.陈慧琳;
我的图像[2].字符项目属性 = "陈慧琳";
我的图像[3].项目图像属性 = _138赵志华高手版.Properties.Resources.温碧霞;
我的图像[3].字符项目属性 = "温碧霞";
我的图像[4].项目图像属性 = _138赵志华高手版.Properties.Resources.徐静蕾;
我的图像[4].字符项目属性 = "徐静蕾";
我的图像[5].项目图像属性 = _138赵志华高手版.Properties.Resources.徐若宣;
我的图像[5].字符项目属性 = "徐若瑄";
我的图像[6].项目图像属性 = _138赵志华高手版.Properties.Resources.张柏芝;
我的图像[6].字符项目属性 = "张柏芝";
我的图像[7].项目图像属性 = _138赵志华高手版.Properties.Resources.张曼玉;
我的图像[7].字符项目属性 = "张曼玉";
foreach (图片列表 itm in 我的图像)
{
listBox1.Items.Add(itm);
}
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0)
return;
Graphics g = e.Graphics;
Rectangle re = listBox1.GetItemRectangle(e.Index);
re.X += 1;
re.Y += 1;
re.Height = 图片列表.图像长度;
re.Width = 图片列表.图像宽度;
图片列表 my = (图片列表)listBox1.Items[e.Index];
g.DrawImage(my.项目图像属性, re);
g.DrawString(my.字符项目属性, new Font("方正字迹-童体硬笔字体", 36),
Brushes.Red, re.X + 图片列表.图像宽度属性 + 4, re.Y + 8);
re = listBox1.GetItemRectangle(e.Index);
re.Width -= 1;
re.Height -= 1;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
g.DrawRectangle(Pens.Red, re);
else
g.DrawRectangle(Pens.LightGray, re);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
图片列表 my = (图片列表)listBox1.Items[listBox1.SelectedIndex];
namelabel .Text = my.字符项目属性;
pictureBox1.Image = my.项目图像属性;
}
}
代码都子啊这了,出错的就在红色部分,报错信息是
未处理的“System.NullReferenceException”类型的异常出现在 138赵志华高手版.exe 中。
其他信息: 未将对象引用设置到对象的实例。
...全文
相关推荐
未将对象引用设置到对象的实例 (System.NullReferenceException)
下面基础的解释一下这错误: 1:本质上的错误: 代码如下: object a;//a是Null对象 protected void Page_Load(object sender, EventArgs e) { a.ToString();//调用一个Null对象的方法 } 当然啦!结果就如下图了: 这么赤裸裸的写出这种代码,不太容易,通常更倾向于下面一种: 2:通常性的错误: 示例1:一个过滤某些字符的函数: 代码如下: public static string FilterValue(string value) { string[] filterChar = new string[]
C#异常处理总结及简单实例
主要介绍了C#异常处理总结及简单实例的相关资料,需要的朋友可以参考下
C# 异常处理典型实例
内容索引:C#源码,综合实战,异常处理 C# 异常处理典型实例,分别演示C#如何处理除0异常、无效对象异常以及自定义异常的操作,学会异常处理将会使你的程序运行更加稳定,因些本实例将非常有用处。
VS2005(c#)项目调试问题解决方案集锦(三)
VS2005(c#)项目调试问题解决方案集锦
System.NullReferenceException(解决方案).md
项目中碰到的,记录一下解决方案


