.NET 4.x При streamreader.ReadLine() выскакивает ошибка object reference not set to an instance of object - C#

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

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

Здраствуйте, у меня вдруг появилась такая проблема, что при streamreader.ReadLine() выскакивает ошибка object reference not set to an instance of object вот код процедуры:
Листинг программы
  1. public void ReadFile()
  2. {
  3. StreamReader streamreader = new StreamReader("Exhibitions.txt");
  4. FileInfo file = new FileInfo("Exhibitions.txt");
  5. First();
  6. _current = 0;
  7. string hourformat = "t";
  8. string dateformat = "dd.MM.yyyy";
  9. while (!streamreader.EndOfStream)
  10. {
  11. _exhibitions[_current].set_name_exh(streamreader.ReadLine());
  12. _exhibitions[_current].set_description_exh(streamreader.ReadLine());
  13. _exhibitions[_current].set_category_exh(streamreader.ReadLine());
  14. _exhibitions[_current].set_autor_exh(streamreader.ReadLine());
  15. _exhibitions[_current].set_start_date_exh(DateTime.ParseExact(streamreader.ReadLine(), dateformat, System.Globalization.CultureInfo.InvariantCulture));
  16. _exhibitions[_current].set_end_date_exh(DateTime.ParseExact(streamreader.ReadLine(), dateformat, System.Globalization.CultureInfo.InvariantCulture));
  17. _exhibitions[_current].set_start_hour_exh(DateTime.ParseExact(streamreader.ReadLine(), hourformat, System.Globalization.CultureInfo.InvariantCulture));
  18. _exhibitions[_current].set_end_hour_exh(DateTime.ParseExact(streamreader.ReadLine(), hourformat, System.Globalization.CultureInfo.InvariantCulture));
  19. // out_all_result(_exhibitions[_current]);
  20. _current++;
  21. }
  22. streamreader.Close();
  23. }
по началу ругаеться только не парвую строку считывания, но думаю что и на все остальные так же будет. не могу понять в чем же проблема, т.к. конструктор класа таков:
Листинг программы
  1. public Exhibitions()
  2. {
  3. name_exh = "";
  4. description_exh = "";
  5. category_exh = "";
  6. autor_exh = "";
  7. start_date_exh = DateTime.Now;
  8. end_date_exh = DateTime.Now;
  9. start_hour_exh = DateTime.Now;
  10. end_hour_exh = DateTime.Now;
  11. }
а масив даных таков:
Листинг программы
  1. public Exhibitions[] _exhibitions = new Exhibitions[50];
  2. //даже делал так пере запуском процедуры
  3. private Exhibitions exhibitions;
  4. public void arraies()
  5. {
  6. exhibitions = new Exhibitions();
  7. for(int i = 0;i<50;i++)
  8. {
  9. _exhibitions[i] = exhibitions;
  10. }
  11. }
ничего не помогает( мои сэтеры:
Листинг программы
  1. public void set_name_exh(string name)
  2. {
  3. name_exh = name;
  4. }
  5. public void set_description_exh(string description)
  6. {
  7. description_exh = description;
  8. }
  9. public void set_category_exh(string category)
  10. {
  11. category_exh = category;
  12. }
  13. public void set_autor_exh(string autor)
  14. {
  15. autor_exh = autor;
  16. }
  17. public void set_start_date_exh(DateTime start_date)
  18. {
  19. start_date_exh = start_date;
  20. }
  21. public void set_end_date_exh(DateTime end_date)
  22. {
  23. end_date_exh = end_date;
  24. }
  25. public void set_start_hour_exh(DateTime start_hour)
  26. {
  27. start_hour_exh = start_hour;
  28. }
  29. public void set_end_hour_exh(DateTime end_hour)
  30. {
  31. end_hour_exh = end_hour;
  32. }
а так же описание проблеми:
System.NullReferenceException не обработано HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=analysis system exhibiting StackTrace: at analysis_system_exhibiting.Exh_iter.ReadFile() in c:\Users\_DarkSiR_\Desktop\курсова\analysis system exhibiting\analysis system exhibiting\Exhibitions.cs:line 436 at analysis_system_exhibiting.Form1.button4_Click(Object sender, EventArgs e) in c:\Users\_DarkSiR_\Desktop\курсова\analysis system exhibiting\analysis system exhibiting\Form1.cs:line 108 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMes sageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at analysis_system_exhibiting.Program.Main() in c:\Users\_DarkSiR_\Desktop\курсова\analysis system exhibiting\analysis system exhibiting\Program.cs:line 19 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
где 108 рядок это рядок вызова процедуры, а 436 это :
Листинг программы
  1. _exhibitions[_current].set_name_exh(streamreader.ReadLine());
помогите пожалуйста, ато я только недавно начал изучать С#, а полазив по форумах с похожей проблемой все равно не смог с ней справится.
Листинг программы
  1. public void WriteFile(string name, string description, string category, string autor, string start_date, string end_date, string start_hour, string end_hour)
  2. {
  3. StreamWriter streamwriter;
  4. FileInfo file = new FileInfo("Exhibitions.txt");
  5. streamwriter = file.AppendText();
  6. streamwriter.WriteLine(name);
  7. streamwriter.WriteLine(description);
  8. streamwriter.WriteLine(category);
  9. streamwriter.WriteLine(autor);
  10. streamwriter.WriteLine(start_date);
  11. streamwriter.WriteLine(end_date);
  12. streamwriter.WriteLine(start_hour);
  13. streamwriter.WriteLine(end_hour);
  14. streamwriter.Close();
  15. }
  16. //которая вызывается вот сдесь:
  17. private void button1_Click(object sender, EventArgs e)
  18. {
  19. iter = new Exh_iter();
  20. string hourformat = "t";
  21. string dateformat = "dd.MM.yyyy";
  22. if ((this.textBox1.Text != "") && (this.textBox2.Text != "") &&(this.textBox3.Text != "") &&(this.textBox4.Text != "") &&(this.textBox5.Text != "") &&(this.textBox6.Text != "") &&(this.textBox7.Text != "") &&(this.richTextBox1.Text != ""))
  23. {
  24. string name = this.textBox1.Text;
  25. string description = this.richTextBox1.Text;
  26. string category = this.textBox2.Text;
  27. string autor = this.textBox7.Text;
  28. string start_date = this.textBox3.Text;
  29. string end_date = this.textBox4.Text;
  30. string start_hour = this.textBox5.Text;
  31. string end_hour = this.textBox6.Text;
  32. iter.WriteFile(name, description, category, autor, start_date, end_date, start_hour, end_hour);
  33. this.Hide();
  34. this.Close();//кстати эта функция тоже почему то не работает, но хоть ошибку не выбивает, просто не закрывает форму. ниодну.
  35. Form1 form1 = new Form1();
  36. form1.HideToolStripMenuItem();
  37. form1.ShowDialog();
  38. }
  39. else
  40. {
  41. Form6 form6 = new Form6();
  42. form6.ShowDialog();
  43. }
  44. }

Решение задачи: «.NET 4.x При streamreader.ReadLine() выскакивает ошибка object reference not set to an instance of object»

textual
Листинг программы
  1. for (i = 0; i < 50; i++)
  2.                 {
  3.                     if (search_result[i] == null) break;
  4.                     this.richTextBox1.Text += iter.out_all_result(search_result[i]);
  5.                    
  6.                 }

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


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

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

11   голосов , оценка 4.182 из 5

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

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

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