c#string发起到“00-00-00-00-00-00-00-00-00-00-00-00”

时间:2017-03-11 16:20:35

标签: c# .net

所以我试图尝试保存在磁盘上的哈希表。当我尝试调试和调试器获得 POINT 1 时,我打开变量并看到DataKey值获得异常'this.Key' threw an exception of type 'System.NullReferenceException'。我转到 POINT 2 ,现在Data获得"00-00-00-00-00-00-00-00-00-00-00-00"的值。 那么奇怪的价值来自何处?我还在Data获取并设置了断点,但是当这些点没有被击中时。

class HashtableFile
    {
        class Hashentry
        {
            const int KEY_BYTES = 4;
            const int DATA_BYTES = 12;
            public int? Key
            {
                get
                {
                    byte[] bytes = new byte[KEY_BYTES];
                    fs.Seek((KEY_BYTES + DATA_BYTES) * Index, SeekOrigin.Begin);
                    fs.Read(bytes, 0, KEY_BYTES);
                    int data = BitConverter.ToInt32(bytes, 0);
                    return data;
                }
                set
                {
                    byte[] bytes = new byte[KEY_BYTES];
                    int key = value.GetValueOrDefault(0);
                    bytes = BitConverter.GetBytes(key);
                    fs.Seek(KEY_BYTES * Index, SeekOrigin.Begin);
                    fs.Write(bytes, 0, KEY_BYTES);
                }
            }
            public string Data
            { 
                get
                {
                    byte[] bytes = new byte[DATA_BYTES];
                    //seek key too
                    fs.Seek(((KEY_BYTES + DATA_BYTES) * Index) + KEY_BYTES, SeekOrigin.Begin);
                    fs.Read(bytes, 0, DATA_BYTES);
                    string data = BitConverter.ToString(bytes, 0);
                    return data;
                }
                set
                {
                    byte[] bytes = new byte[DATA_BYTES];
                    string data = value ?? string.Empty;
                    data = data.Insert(data.Length, new string(' ', DATA_BYTES - data.Length));
                    bytes = Encoding.ASCII.GetBytes(data);
                    fs.Seek(((KEY_BYTES + DATA_BYTES) * Index) + KEY_BYTES, SeekOrigin.Begin);
                    fs.Write(bytes, 0, DATA_BYTES);
                }
            }
            private int Index;
            private FileStream fs;
            public Hashentry(int? key, string data, int index, FileStream fs)//POINT 1
            {
                this.fs = fs;
                Index = index; // POINT 2
                Key = key;
                Data = data;
            }
            public int GetIndex()
            {
                return Index;
            }
            public int? GetKey()
            {
                return Key;
            }
            public string GetData()
            {
                return Data;
            }
        }

        private int maxSize;
        private Hashentry[] table;
        private string file;
        private FileStream fs;
        public HashtableFile(int size, string filename)
        {
            file = filename;
            if (File.Exists(file))
                File.Delete(file);
            fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            maxSize = size;
            table = new Hashentry[maxSize];
            for (int i = 0; i < maxSize; i++)
            {
                table[i] = new Hashentry(null, null, i, fs);
            }
        }
}

2 个答案:

答案 0 :(得分:1)

看看

public int? Key
{
    get
    {
        byte[] bytes = new byte[KEY_BYTES];
        fs.Seek((KEY_BYTES + DATA_BYTES) * Index, SeekOrigin.Begin);
你知道吗? Key getter的第二行正在使用fs。如果fs == null(类的字段的默认未初始化值),则NullReferenceException如果您尝试使用Key

BUT

public Hashentry(int? key, string data, int index, FileStream fs)//POINT 1
{
    this.fs = fs;
    Index = index; // POINT 2

构造函数的第一行设置fs!所以不再NullReferenceException。所以在POINT 1this.fs == nullPOINT 2this.fs = something

答案 1 :(得分:0)

问题是那个

string data = BitConverter.ToString(bytes, 0);

将字节直接转换为字符串,而不是 对应的 字符。解决方案是将此行更改为:

string data = Encoding.ASCII.GetString(bytes);