【发布时间】:2013-11-23 23:09:11
【问题描述】:
Trying to figure out why this isn't working, the list is to retrieve photos using the combobox item (which lists local HDDs root address as items) when selected that item it is converted into a string and supposed to be used as GetFiles 方法的路径但在运行时在 (string path = ) 行上搞砸了,我得到“对象引用未设置为对象的实例”非常感谢如果有人能告诉我出了什么问题
public List<Photos> LoadImages ///List Retrieves and Loads Photos
{
get
{
List<Photos> Image = new List<Photos>();
string path = HDDSelectionBox.SelectedItem.ToString(); //ComboBox SelectedItem Converted To String As Path
foreach (string filename in Directory.GetFiles(path, "*jpg"))
{
try
{
Image.Add( //Add To List
new Photos(
new BitmapImage(
new Uri(filename)),
System.IO.Path.GetFileNameWithoutExtension(filename)));
}
catch { } //Skips Any Image That Isn't Image/Cant Be Loaded
}
return Image;
}
}
【问题讨论】:
-
这很可能意味着
HDDSelectionBox或HDDSelectionBox.SelectedItem为空。你检查过吗? -
mm 我读到了,但是因为我正在学习这对我来说是新的,我的目标是仅在组合框不为空时加载图像,即仅在用户选择了一个项目之后从组合框中,我感觉它在程序加载时制作了一个列表,然后当然会以 null 开头