Проблема со свойствами: Object reference not set to an instance of an object - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте, пишу вот такой код:
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication1
  6. {
  7. class textFragmented{
  8. public int numberGrapheme { get; set; }//номер кластера
  9. public string typeTextFragmented { get; set; }//номер кластера
  10. } //конец класса textFragmented
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. textFragmented[] textFrag = new textFragmented[0];
  16. Array.Resize(ref textFrag, textFrag.Length + 1);
  17. textFrag[textFrag.Length - 1].numberGrapheme = 1;
  18. textFrag[textFrag.Length - 1].typeTextFragmented = "NON";
  19. Console.WriteLine(textFrag[0].numberGrapheme);
  20. Console.WriteLine(textFrag[0].typeTextFragmented);
  21. Console.ReadLine();();
  22. }
  23. }
  24. }
На строчке:
Листинг программы
  1. textFrag[textFrag.Length - 1].numberGrapheme = 1;
выдаёт ошибку: Object reference not set to an instance of an object. Если пишу:
Листинг программы
  1. static void Main(string[] args)
  2. {
  3. textFragmented textFrag = new textFragmented();
  4. textFrag.numberGrapheme = 1;
  5. textFrag.typeTextFragmented = "NON";
  6. Console.WriteLine(textFrag.numberGrapheme);
  7. Console.WriteLine(textFrag.typeTextFragmented);
  8. Console.ReadLine();
  9. }
Тогда такой ошибки не возникает. По каким причинам появляется эта ошибка? И как можно исправить, или обойти данную проблему? Мне нужно именно массив textFragmented[].

Решение задачи: «Проблема со свойствами: Object reference not set to an instance of an object»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class textFragmented{
  9.  
  10.         public int numberGrapheme { get; set; }//номер кластера
  11.         public string typeTextFragmented { get; set; }//номер кластера
  12.     } //конец класса textFragmented
  13.  
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             textFragmented[] textFrag = new textFragmented[0];
  19.  
  20.             Array.Resize(ref textFrag, textFrag.Length + 1);
  21.             textFrag[textFrag.Length - 1] = new textFragmented(); //тут
  22.             textFrag[textFrag.Length - 1].numberGrapheme = 1;
  23.             textFrag[textFrag.Length - 1].typeTextFragmented = "NON";
  24.             Console.WriteLine(textFrag[0].numberGrapheme);
  25.             Console.WriteLine(textFrag[0].typeTextFragmented);
  26.             Console.ReadLine();
  27.         }
  28.     }
  29. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 3.875 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы