Results 1 to 5 of 5
  1. #1

    Default C# Problem: Protected Variable


    ok i have a huge problem...everytime i access a protected variable outside the class where it was declared in it gives me the error message of
    "object reference not set to an instance of an object"...if i access it in the same class as i declared it in it doesnt give me the error...so probably the problem is in the inhereting of the variable from the class to another class....heres the code

    Memory2.cs

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SML
    {
        class Memory2
        {
             protected int[] memory;
            
            /*** REGISTERS ***/
            protected int accumulator;
            protected int programCounter;
            protected int instructionRegister;
            protected int operationCode;
            protected int operand;
           
    
            public void Memory()
            {
                Initialize();
            }
            
            public void Initialize()
            {
                int index = 0;
                memory = new int[100];
                try
                {
                    for (; index < 100; index++)
                    {
                        memory[index] = 0;
                    }
                }
                catch(Exception e){
                    Console.WriteLine("Message: "+e.ToString());
                }
            }
    
            public void Dump()
            {
                int i = 0,dummy = 0, dummyAccum = 0;
                Console.WriteLine("REGISTERS:");
                if (accumulator >= 0)
                {
                    if (accumulator.ToString().Length == 1)
                        Console.WriteLine("Accumulator: \t      +000" + accumulator);
                    else if (accumulator.ToString().Length == 2)
                        Console.WriteLine("Accumulator: \t      +00" + accumulator);
                    else if (accumulator.ToString().Length == 3)
                        Console.WriteLine("Accumulator: \t      +0" + accumulator);
                    else
                        Console.WriteLine("Accumulator: \t      +" + accumulator);
                }
                else
                {
                    dummyAccum = Math.Abs(accumulator);
                    if (dummyAccum.ToString().Length == 1)
                        Console.WriteLine("Accumulator: \t      -000" + dummyAccum);
                    else if (dummyAccum.ToString().Length == 2)
                        Console.WriteLine("Accumulator: \t      -00" + dummyAccum);
                    else if (dummyAccum.ToString().Length == 3)
                        Console.WriteLine("Accumulator: \t      -0" + dummyAccum);
                    else
                        Console.WriteLine("Accumulator: \t      -" + dummyAccum);
                }
                if(programCounter.ToString().Length == 1)
                 Console.WriteLine("Program Counter: \t 0" + programCounter); 
                else
                 Console.WriteLine("Program Counter: \t " + programCounter);    
                if (instructionRegister >= 0)
                {
                    if (instructionRegister.ToString().Length == 1)
                        Console.WriteLine("Instruction Register: +000" + instructionRegister);
                    else if (instructionRegister.ToString().Length == 2)
                        Console.WriteLine("Instruction Register: +00" + instructionRegister);
                    else if (instructionRegister.ToString().Length == 3)
                        Console.WriteLine("Instruction Register: +0" + instructionRegister);
                    else
                        Console.WriteLine("Instruction Register: +" + instructionRegister);
                }
                else
                {
                    instructionRegister = Math.Abs(instructionRegister);
                    if (instructionRegister.ToString().Length == 1)
                        Console.WriteLine("Instruction Register: -000" + instructionRegister);
                    else if (instructionRegister.ToString().Length == 2)
                        Console.WriteLine("Instruction Register: -00" + instructionRegister);
                    else if (instructionRegister.ToString().Length == 3)
                        Console.WriteLine("Instruction Register: -0" + instructionRegister);
                    else
                        Console.WriteLine("Instruction Register: -" + instructionRegister);
                }
                if(operationCode.ToString().Length == 1)
                 Console.WriteLine("Operation Code: \t 0"+ operationCode);
                else
                 Console.WriteLine("Operation Code: \t " + operationCode);
                if(operand.ToString().Length == 1)
                 Console.WriteLine("Operand: \t\t 0"+ operand);
                else
                 Console.WriteLine("Operand: \t\t " + operand);
                Console.WriteLine("\nMEMORY: ");
                Console.Write("      0       1       2       3       4       5       6       7       8       9\n");
                for (i = 0;i<100 ;i++)
                {
                    if (memory[i] >= 0)
                    {
                        displayMemoryNumber(i);
                        if (memory[i].ToString().Length == 1)
                         Console.Write("+000" + memory[i] + "   ");
                        else if (memory[i].ToString().Length == 2)
                         Console.Write("+00" + memory[i] + "   ");
                        else if (memory[i].ToString().Length == 3)
                         Console.Write("+0" + memory[i] + "   ");
                        else
                         Console.Write("+" + memory[i] + "   ");
                    }
                    else
                    {
                        dummy = Math.Abs(memory[i]);
                        if (dummy.ToString().Length == 1)
                         Console.Write("-000" + dummy + "   ");
                        else if (dummy.ToString().Length == 2)
                         Console.Write("-00" + dummy + "   ");
                        else if (dummy.ToString().Length == 3)
                         Console.Write("-0" + dummy + "   ");
                        else
                         Console.Write("-" + dummy + "   ");
                    }
                }
               
            }
    
    
            
            private void displayMemoryNumber(int index)
            {
                switch(index)
                {
                    case 0: Console.SetCursorPosition(0, 9);
                            Console.Write(index + "  ");
                            break;
                    case 10: Console.SetCursorPosition(0, 10);
                            Console.Write(index + " ");
                            break;
                    case 20: Console.SetCursorPosition(0, 11);
                            Console.Write(index + " ");
                            break;
                    case 30: Console.SetCursorPosition(0, 12);
                            Console.Write(index + " ");
                            break;
                    case 40: Console.SetCursorPosition(0, 13);
                            Console.Write(index + " ");
                            break;
                    case 50: Console.SetCursorPosition(0, 14);
                            Console.Write(index + " ");
                            break;
                    case 60: Console.SetCursorPosition(0, 15);
                            Console.Write(index + " ");
                            break;
                    case 70: Console.SetCursorPosition(0, 16);
                            Console.Write(index + " ");
                            break;
                    case 80: Console.SetCursorPosition(0, 17);
                            Console.Write(index + " ");
                            break;
                    case 90: Console.SetCursorPosition(0, 18);
                            Console.Write(index + " ");
                            break;
                }
               
            }
           
        
        }
    }
    decode.cs

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SML
    {
        class decode:Memory2
        {
            public void LoadtoMemory(int address, int value)
            {
                //check if the memory address is valid
                if ((address >= 0) && (address <= 99))
                    memory[address] = value;
                else
                    Console.Write("\nError: Invalid address at " + address + ".\n");
            }
    
            public void display()
            {
                programCounter = 0;
                for (; programCounter <= 99; programCounter++)
                {
                    if (memory[programCounter] == 4300)
                    {
                        Console.WriteLine("\t\t\t*******Program Ended*******");
                        break;
                    }
                    else
                    {
    
                        Console.Clear();
                        operationCode = memory[programCounter] / 100;
                        operand = memory[programCounter] % 100;
                        instructionRegister = memory[programCounter];
                        Dump();
                        decode2();
                        Console.ReadLine();
    
                    }
                }
    
            }
            public void display2()
            {
                programCounter = 0;
                for (; programCounter <= 99; programCounter++)
                {
                    if (memory[programCounter] == 4300)
                    {
                        Console.WriteLine("\t\t\t*******Program Ended*******");
                        break;
                    }
    
                    else
                    {
    
                        Console.Clear();
                        operationCode = memory[programCounter] / 100;
                        operand = memory[programCounter] % 100;
                        //Dump();
                        decode2();
    
    
                    }
                }
    
            }
            public void decode2()
            {
                if (operationCode == 10)
                {
                    read();
                }
                else if (operationCode == 11)
                {
                    write();
                }
                else if (operationCode == 20)
                {
                    load();
                }
                else if (operationCode == 21)
                {
                    store();
                }
                if (operationCode == 30)
                {
                    add();
                }
                if (operationCode == 31)
                {
                    subtract();
                }
                if (operationCode == 32)
                {
                    multiply();
                }
                if (operationCode == 33)
                {
                    divide();
                }
                if (operationCode == 40)
                {
    
                }
                if (operationCode == 41)
                {
                    branchneg();
                }
                if (operationCode == 42)
                {
    
                }
                if (operationCode == 43)
                {
    
                }
            }
            public void branch()
            {
                programCounter = operand - 1;
            }
    
            public void branchneg()
            {
                if (accumulator < 0)
                {
                    programCounter = operand - 1;
    
                }
    
            }
            public void branchzero()
            {
                if (accumulator == 0)
                {
                    programCounter = operand - 1;
    
                }
    
            }
    
            public void read()
            {
    
    
                Console.Write("INPUT: ");
                memory[operand] = int.Parse(Console.ReadLine());
            }
            public void write()
            {
                Console.WriteLine(memory[operand]);
            }
            public void load()
            {
                accumulator = memory[operand];
            }
            public void store()
            {
                memory[operand] = accumulator;
            }
            public void add()
            {
                accumulator = accumulator + memory[operand];
            }
            public void subtract()
            {
                accumulator = accumulator - memory[operand];
            }
            public void multiply()
            {
                accumulator = accumulator * memory[operand];
            }
            public void divide()
            {
                accumulator = accumulator / memory[operand];
            }
        }
    }

    pls help guyz...every reply would be appreciated...nosebleed na kaayo ko ani gamay na lang kuwang

  2. #2
    Sakto man imung paggamit sa protected field. Unsa nga specific line ang ni-give og error?

    Without knowing the whole problem, I guess ang sayop kay ang memory nga field. Memory is an array of int. Each element in the array may be a stranger to the decode class.

  3. #3
    as i can see in your code, the Memory2 class doesn't have a constructor...try to put a constructor on it..

    if dili gihapon mo-work add public modifier to your Memory2 class

  4. #4
    tried putting the constructor but nothing change....i cant change the modifier to public since the requirement is to make it protected T.T

  5. #5
    in your decode class add a constructor that calls the Initialize method of your base class (Memory2)
    Code:
    public decode()
    {
         base.Initialize();
    }

  6.    Advertisement

Similar Threads

 
  1. SGPT Problem
    By Ferl in forum Fitness & Health
    Replies: 78
    Last Post: 03-25-2013, 11:44 AM
  2. The disk is write protected (usb PROBLEM) Help!
    By muchacha00 in forum Computer Hardware
    Replies: 16
    Last Post: 04-07-2012, 09:52 PM
  3. Replies: 221
    Last Post: 06-02-2009, 01:22 AM
  4. Playstation 2 Problem
    By baldog in forum Gizmos & Gadgets (Old)
    Replies: 18
    Last Post: 03-02-2006, 08:28 AM
  5. m125 problem
    By eliel73173 in forum Gizmos & Gadgets (Old)
    Replies: 6
    Last Post: 04-29-2005, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top