【发布时间】:2015-12-19 13:09:23
【问题描述】:
我有一个方法可以从数组中删除所有旧图片并添加新图片,但它会在最后一行抛出 System.NullReferenceException。
PictureBox[] selectedCards = new PictureBox[0];
byte selectedCardsCount = 0;
int selectedCardsIndex = 0;
private void AddSelectedCard(int index, PictureBox newCard)
{
if (selectedCards.Length != 0)
{
foreach (PictureBox card in selectedCards)
{
this.Controls.Remove(card);
}
}
selectedCardsCount++;
selectedCards = new PictureBox[selectedCardsCount];
selectedCards[selectedCardsIndex].Image = new Bitmap(newCard.Image); //The exception is thrown on this line
selectedCardsIndex++;
}
当我调试程序时:
selectedCards[selectedCardsIndex] 为 0(非空)
newCard.Image 是 System.Drawing.Bitmap(也不为空)
【问题讨论】:
-
你的最后一段没有意义。
selectedCards[selectedCardsIndex]的类型是PictureBox,它永远不可能是0。
标签: c# exception picturebox throw