C#在图片框数组中循环-NullReferenceException?
我不明白我做错了什么,但我在尝试循环遍历这个picturebox创建数组时遇到了这个错误。因此,我用了“捉迷藏”来缩小范围,但我仍然不知道出了什么问题C#在图片框数组中循环-NullReferenceException?,c#,arrays,image,loops,picturebox,C#,Arrays,Image,Loops,Picturebox,我不明白我做错了什么,但我在尝试循环遍历这个picturebox创建数组时遇到了这个错误。因此,我用了“捉迷藏”来缩小范围,但我仍然不知道出了什么问题 private System.Windows.Forms.PictureBox[] imgVictim = new PictureBox[3]; //array for victim images public void victimsRun() { victimTimer.Enabled = true; //starts the tim
private System.Windows.Forms.PictureBox[] imgVictim = new PictureBox[3]; //array for victim images
public void victimsRun()
{
victimTimer.Enabled = true; //starts the timer
PictureBox[] victim = new PictureBox[3];
for (int i = 0; i < imgVictim.Length; i++) // 0 - 2
{
try
{
imgVictim[i].Image = Image.FromFile("victim" + i.ToString() + ".png");
}
catch (NullReferenceException)
{
MessageBox.Show("NULL EXECEPTION!");
}
MessageBox.Show(i.ToString());
}
}
private System.Windows.Forms.PictureBox[]imgvitictim=new PictureBox[3]//受害者图像阵列
公共无效受害者RUN()
{
victimTimer.Enabled=true;//启动计时器
PictureBox[]受害者=新PictureBox[3];
for(inti=0;i
有人能帮我吗?在使用之前,您需要创建每个PictureBox
imgVictim[i] = new PictureBox();
试试这个
private System.Windows.Forms.PictureBox[] imgVictim = new PictureBox[3]; //array for victim images
public void victimsRun()
{
victimTimer.Enabled = true; //starts the timer
string fileName = "";
PictureBox[] victim = new PictureBox[3];
for (int i = 0; i < imgVictim.Length; i++) // 0 - 2
{
try
{
fileName = "victim" + i.ToString() + ".png";
if (System.IO.File.Exists(fileName))
{
imgVictim[i] = new PictureBox();
imgVictim[i].Image = Image.FromFile("victim" + i.ToString() + ".png");
}
else
{
// file does not exist or needs a path in front of it
}
}
catch (NullReferenceException)
{
MessageBox.Show("NULL EXECEPTION!");
}
}
}
private System.Windows.Forms.PictureBox[]imgvitictim=new PictureBox[3]//受害者图像阵列
公共无效受害者RUN()
{
victimTimer.Enabled=true;//启动计时器
字符串fileName=“”;
PictureBox[]受害者=新PictureBox[3];
for(inti=0;i
创建一个包含3个picturebox的数组并不意味着您已经创建了3个picturebox。提示:您认为您所显示的代码创建了多少个picturebox
实例?你在哪里调用任何PictureBox
构造函数?我没有为PictureBox创建任何构造函数,它是必需的吗?感谢您的快速响应。我仍然收到异常。我在第一次回复中添加了第二个代码示例,看看这是否有帮助。在微软的会议上,我可能有一段时间无法做出回应。