Проблема со свойствами: Object reference not set to an instance of an object - C#
Формулировка задачи:
Здравствуйте, пишу вот такой код:
На строчке:
выдаёт ошибку: Object reference not set to an instance of an object.
Если пишу:
Тогда такой ошибки не возникает.
По каким причинам появляется эта ошибка? И как можно исправить, или обойти данную проблему? Мне нужно именно массив textFragmented[].
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class textFragmented{
- public int numberGrapheme { get; set; }//номер кластера
- public string typeTextFragmented { get; set; }//номер кластера
- } //конец класса textFragmented
- class Program
- {
- static void Main(string[] args)
- {
- textFragmented[] textFrag = new textFragmented[0];
- Array.Resize(ref textFrag, textFrag.Length + 1);
- textFrag[textFrag.Length - 1].numberGrapheme = 1;
- textFrag[textFrag.Length - 1].typeTextFragmented = "NON";
- Console.WriteLine(textFrag[0].numberGrapheme);
- Console.WriteLine(textFrag[0].typeTextFragmented);
- Console.ReadLine();();
- }
- }
- }
Листинг программы
- textFrag[textFrag.Length - 1].numberGrapheme = 1;
Листинг программы
- static void Main(string[] args)
- {
- textFragmented textFrag = new textFragmented();
- textFrag.numberGrapheme = 1;
- textFrag.typeTextFragmented = "NON";
- Console.WriteLine(textFrag.numberGrapheme);
- Console.WriteLine(textFrag.typeTextFragmented);
- Console.ReadLine();
- }
Решение задачи: «Проблема со свойствами: Object reference not set to an instance of an object»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class textFragmented{
- public int numberGrapheme { get; set; }//номер кластера
- public string typeTextFragmented { get; set; }//номер кластера
- } //конец класса textFragmented
- class Program
- {
- static void Main(string[] args)
- {
- textFragmented[] textFrag = new textFragmented[0];
- Array.Resize(ref textFrag, textFrag.Length + 1);
- textFrag[textFrag.Length - 1] = new textFragmented(); //тут
- textFrag[textFrag.Length - 1].numberGrapheme = 1;
- textFrag[textFrag.Length - 1].typeTextFragmented = "NON";
- Console.WriteLine(textFrag[0].numberGrapheme);
- Console.WriteLine(textFrag[0].typeTextFragmented);
- Console.ReadLine();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д