Please give me advice with error which me depict compiler - Object reference not set to an instance of an object... before I tried to write without mark line and depicted the same error then I found on the net how way shall I perform and It depicts again... So please tell me how way shall remove this error

using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
//using ClassLibrary1;
namespace pruchod
{
    class labyrint
    {
        static void Main (string [] args)
        {
            ConsoleKeyInfo p;
            matice_knihovna exam = new matice_knihovna();
            exam.load();
            p = Console.ReadKey();
        }
    }
   public class matice_knihovna
    {
        public int x;
        public int y;
        public int xx;
        public int yy;
        const int limit = 20;
        public int[,] position;
        int[,] matice=new int[2,2] {{3,2},{22,4}};
        public int bar_name
        {
            get { return 0; }
        }
        public void load()
        {
            Random rnd = new Random();
            int row = 2;
            int cols = 2;
            matice_knihovna foo = new matice_knihovna();
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Console.Write(matice[i,j]);
                }
                Console.WriteLine();
            }
            position[xx, yy] = foo.bar_name;// reason of error
            position[xx, yy] = load_position(x,y);
        }
        public int load_position(int x,int y) //funkce pro zjištění začátku uživatele
        {
            int limit = 20;
            int[,] begin_position=new int[limit,limit];
            int counter = 0;
            Console.Write("Option position");
            while (counter == 2)
            {
                counter++;
                if (counter == 1)
                {
                    x = Console.Read();
                }
                else if (counter == 2)
                {
                    y = Console.Read();
                }
            }
            return begin_position[x, y];
        }
    }
}

Edited 3 Years Ago by pyTony: fixed formatting

You have only declared the two dimensionaly array, but you didnt initialize it.
Try to insert this line of code:

position = new int[1, 1]; //insert this line - to initialize new array!
 position[xx, yy] = foo.bar_name;// reason of error
 position[xx, yy] = load_position(x, y);

Edited 5 Years Ago by Mitja Bonca: n/a

you should create an array object before initialize array

position = new int[1, 1];
put it before your error line.

This article has been dead for over six months. Start a new discussion instead.