1

我在行收到以下错误_dict.Add(id, ar)

你调用的对象是空的

这是我的代码:

public Dictionary<int, byte[]> _dict;

public IDictionary<int, byte[]> dict()
{
    string sp = HttpContext.Current.Server.MapPath("Images");
    DirectoryInfo folder = new DirectoryInfo(sp);
    FileInfo[] files = folder.GetFiles("*.jpg");
    foreach (var file in files)
    {
        string name = file.Name;
        int id = Convert.ToInt32(name.Substring(0, name.Length - 4));
        FileStream fS = new FileStream(sp + "\\" + name, FileMode.Open, FileAccess.Read);
        byte[] ar = new byte[fS.Length];
        fS.Read(ar, 0, (int)fS.Length);
        fS.Close();
        _dict.Add(id, ar);
    }
    return _dict;
}
4

1 回答 1

6

_dict 从未被实例化过。将其更改为:

public Dictionary<int, byte[]> _dict = new Dictionary<int, byte[]>();
于 2013-07-05T18:26:01.957 回答