【问题标题】:Null Refrence Exception Unhandled in my Microprocessor simulator [duplicate]我的微处理器模拟器中未处理的空引用异常[重复]
【发布时间】:2014-10-18 03:18:50
【问题描述】:

所以我的记忆类看起来像这样:

namespace GeminiCore
{
    public class Memory
    {
    public static int[] memory = new int[256];
    public string nextInstruction;
    public static int cacheSize = 8;
    public CPU myCPU;

    public struct frame
    {
        public bool dirtyBit;
        public int isEmpty;
        public int value;
        public int tag;

        public frame(int cacheSize)
        {
            dirtyBit = false;
            isEmpty = 1;
            value = 0;
            tag = 0;
        }
    }

    public int solveMemory(int value, int instr, frame[] myCache)
    {
        Console.WriteLine("I reached the solveMemory!!!");
        int block = value % cacheSize;
        if(instr == 127 || instr == 125 || instr == 124 || instr == 123 
                || instr == 122 || instr == 121|| instr == 120)
        {
            Console.WriteLine("I reached the read section!!!");
            if(myCache[block].isEmpty == 0) //Read hit
                if(myCache[block].tag == value) 
                    return myCache[block].value;
            else
            {
                myCache[block].value = memory[value]; //Read Miss
                myCache[block].tag = value;
                myCache[block].isEmpty = 0;
                Console.WriteLine("Read Miss --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag);
                return myCache[block].value;
            }
        }
        else
        {
            Console.WriteLine("I reached the write section!!!");
            if (myCache[block].isEmpty == 1) //Write Miss
            {
                Console.WriteLine("Write Miss --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag);
                memory[value] = myCPU.ACC;
            }
            else
            {
                if (myCache[block].dirtyBit == false)
                {
                    if (myCache[block].tag != value)
                    {
                        myCache[block].value = myCPU.ACC; //Write Hit
                        myCache[block].dirtyBit = true;
                        myCache[block].tag = value;
                        Console.WriteLine("Write Hit --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag);

                    }
                }
                else
                {
                    memory[myCache[block].tag] = myCache[block].value;
                    myCache[block].value = myCPU.ACC;
                    myCache[block].tag = value;
                    myCache[block].dirtyBit = false;
                    Console.WriteLine("Write Hit --- The Cache is as follows: block = " + block + " the value at this block is: " + myCache[block].value + " the tag at this block is: " + myCache[block].tag);
                }

            }
         }

        return value;
    }
}

}

然后我有我的 CPU 类,我将发布一个小的 sn-p 错误发生的位置:

public Memory myMemory;
public static Memory.frame[] myCache = new Memory.frame[Memory.cacheSize];
public void doInstruction()
    {   
        var instr = (finalCodes[i] >> 9) & 127; //Parses op code to find instruction
        var immed = (finalCodes[i] >> 8) & 1; //Parses op code to find immediate value
        var value = (finalCodes[i] & 255); //Parse op code to find value

        foreach (Memory.frame x in myCache)
        {
            Console.WriteLine("Dirtybit: " + x.dirtyBit + " isEmpty: " + x.isEmpty + " tag: " + x.tag + " value: " + x.value);
        }

        switch (instr)
        {
            case (127): //LDA instruction
                if (immed == 1) 
                    ACC = value;
                else if (immed == 0) 
                    //ACC = Memory.memory[value];
                    ACC = myMemory.solveMemory(value, instr, myCache);
                break;
            case (126): //STA instruction
                if (immed == 0)
                {
                    Console.WriteLine("The value is: " + value + " The instruction is: " + instr);
                    foreach (Memory.frame x in myCache)
                    {
                        Console.WriteLine("Dirtybit: " + x.dirtyBit + " isEmpty: " + x.isEmpty + " tag: " + x.tag + " value: " + x.value);
                    }
                    //Memory.memory[value] = ACC;
                    myMemory.solveMemory(value, instr, myCache);

                }

所以这是我的问题,当我运行接受汇编代码并将其转换为二进制然后运行代码的测试时,当我到达第二个命令“sta”时,它应该转到该行:

 myMemory.solveMemory(value, instr, myCache);

当它到达那个点时,它会给我一个空引用异常。如您所见,我有一些命令行输出可以尝试查看它的去向。它在该行之前打印出 myCache 的内容。但是,它没有到达solveMemory函数中的任何调试语句,甚至第一个说:

Console.WriteLine("I reached the solveMemory!!!");

我不太确定是什么导致了这个错误,我已经研究了很长时间了。希望你们中的一个人能够找到我到底在哪里搞砸了。谢谢你的时间。

【问题讨论】:

  • 好吧,我知道空引用异常是什么,我只是不知道为什么我的代码中会出现它
  • 也许我遗漏了一些东西,但你实际上是在哪里分配给myMemory
  • 哇,我不敢相信。只需要说 Memory myMemory = new Memory();
  • 您可以将其发布为答案,以便获得荣誉。感谢您注意到这一点
  • 很高兴我们为您解决了问题,尽管我看到您已经发布了自己的答案。不过,将其标记为与我最初链接的问题的重复可能不是一个坏主意。这有一个更完整的解释,因此它更有可能帮助未来处于类似困境的用户。

标签: c# visual-studio nullreferenceexception processor


【解决方案1】:
public Memory myMemory;

应该是:

public Memory myMemory = new Memory();

【讨论】:

    猜你喜欢
    • 2013-12-25
    • 2015-11-15
    • 1970-01-01
    • 2021-03-07
    • 2013-03-31
    • 2018-10-16
    • 2014-06-24
    • 1970-01-01
    相关资源
    最近更新 更多